hdl.dsl: py3.12+: turn off heuristic warning on ~True and ~False.

There is now an upstream deprecation warning for the same.
We don't have to duplicate it.
This commit is contained in:
Catherine 2023-06-01 18:22:24 +00:00
parent 58b8acac0d
commit a4402b507f
2 changed files with 21 additions and 17 deletions

View file

@ -3,6 +3,7 @@ from contextlib import contextmanager, _GeneratorContextManager
from functools import wraps
from enum import Enum
import warnings
import sys
from .._utils import flatten, bits_for
from .. import tracer
@ -210,7 +211,8 @@ class Module(_ModuleBuilderRoot, Elaboratable):
def _check_signed_cond(self, cond):
cond = Value.cast(cond)
if cond.shape().signed:
if sys.version_info < (3, 12, 0) and cond.shape().signed:
# TODO(py3.11): remove; ~True is a warning in 3.12+, finally!
warnings.warn("Signed values in If/Elif conditions usually result from inverting "
"Python booleans with ~, which leads to unexpected results. "
"Replace `~flag` with `not flag`. (If this is a false positive, "

View file

@ -1,5 +1,6 @@
# amaranth: UnusedElaboratable=no
import sys
from collections import OrderedDict
from amaranth.hdl.ast import *
@ -324,27 +325,28 @@ class DSLTestCase(FHDLTestCase):
)
""")
def test_If_signed_suspicious(self):
m = Module()
with self.assertWarnsRegex(SyntaxWarning,
(r"^Signed values in If\/Elif conditions usually result from inverting Python "
if sys.version_info < (3, 12): # upstream warning in 3.12!
def test_If_signed_suspicious(self):
m = Module()
with self.assertWarnsRegex(SyntaxWarning,
r"^Signed values in If\/Elif conditions usually result from inverting Python "
r"booleans with ~, which leads to unexpected results\. Replace `~flag` with "
r"`not flag`\. \(If this is a false positive, silence this warning with "
r"`m\.If\(x\)` → `m\.If\(x\.bool\(\)\)`\.\)$")):
with m.If(~True):
pass
r"`m\.If\(x\)` → `m\.If\(x\.bool\(\)\)`\.\)$"):
with m.If(~True):
pass
def test_Elif_signed_suspicious(self):
m = Module()
with m.If(0):
pass
with self.assertWarnsRegex(SyntaxWarning,
(r"^Signed values in If\/Elif conditions usually result from inverting Python "
def test_Elif_signed_suspicious(self):
m = Module()
with m.If(0):
pass
with self.assertWarnsRegex(SyntaxWarning,
r"^Signed values in If\/Elif conditions usually result from inverting Python "
r"booleans with ~, which leads to unexpected results\. Replace `~flag` with "
r"`not flag`\. \(If this is a false positive, silence this warning with "
r"`m\.If\(x\)` → `m\.If\(x\.bool\(\)\)`\.\)$")):
with m.Elif(~True):
pass
r"`m\.If\(x\)` → `m\.If\(x\.bool\(\)\)`\.\)$"):
with m.Elif(~True):
pass
def test_if_If_Elif_Else(self):
m = Module()