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 functools import wraps
from enum import Enum from enum import Enum
import warnings import warnings
import sys
from .._utils import flatten, bits_for from .._utils import flatten, bits_for
from .. import tracer from .. import tracer
@ -210,7 +211,8 @@ class Module(_ModuleBuilderRoot, Elaboratable):
def _check_signed_cond(self, cond): def _check_signed_cond(self, cond):
cond = Value.cast(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 " warnings.warn("Signed values in If/Elif conditions usually result from inverting "
"Python booleans with ~, which leads to unexpected results. " "Python booleans with ~, which leads to unexpected results. "
"Replace `~flag` with `not flag`. (If this is a false positive, " "Replace `~flag` with `not flag`. (If this is a false positive, "

View file

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