diff --git a/amaranth/hdl/dsl.py b/amaranth/hdl/dsl.py index feabef1..7edb03b 100644 --- a/amaranth/hdl/dsl.py +++ b/amaranth/hdl/dsl.py @@ -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, " diff --git a/tests/test_hdl_dsl.py b/tests/test_hdl_dsl.py index d08878e..9fc210f 100644 --- a/tests/test_hdl_dsl.py +++ b/tests/test_hdl_dsl.py @@ -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()