Skip to content

Commit 2c0f844

Browse files
committed
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.
1 parent 32c323a commit 2c0f844

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

amaranth/hdl/dsl.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from functools import wraps
44
from enum import Enum
55
import warnings
6+
import sys
67

78
from .._utils import flatten, bits_for
89
from .. import tracer
@@ -210,7 +211,8 @@ def _set_ctrl(self, name, data):
210211

211212
def _check_signed_cond(self, cond):
212213
cond = Value.cast(cond)
213-
if cond.shape().signed:
214+
if sys.version_info < (3, 12, 0) and cond.shape().signed:
215+
# TODO(py3.11): remove; ~True is a warning in 3.12+, finally!
214216
warnings.warn("Signed values in If/Elif conditions usually result from inverting "
215217
"Python booleans with ~, which leads to unexpected results. "
216218
"Replace `~flag` with `not flag`. (If this is a false positive, "

tests/test_hdl_dsl.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# amaranth: UnusedElaboratable=no
22

3+
import sys
34
from collections import OrderedDict
45

56
from amaranth.hdl.ast import *
@@ -324,27 +325,28 @@ def test_If_wide(self):
324325
)
325326
""")
326327

327-
def test_If_signed_suspicious(self):
328-
m = Module()
329-
with self.assertWarnsRegex(SyntaxWarning,
330-
(r"^Signed values in If\/Elif conditions usually result from inverting Python "
328+
if sys.version_info < (3, 12): # upstream warning in 3.12!
329+
def test_If_signed_suspicious(self):
330+
m = Module()
331+
with self.assertWarnsRegex(SyntaxWarning,
332+
r"^Signed values in If\/Elif conditions usually result from inverting Python "
331333
r"booleans with ~, which leads to unexpected results\. Replace `~flag` with "
332334
r"`not flag`\. \(If this is a false positive, silence this warning with "
333-
r"`m\.If\(x\)` → `m\.If\(x\.bool\(\)\)`\.\)$")):
334-
with m.If(~True):
335-
pass
335+
r"`m\.If\(x\)` → `m\.If\(x\.bool\(\)\)`\.\)$"):
336+
with m.If(~True):
337+
pass
336338

337-
def test_Elif_signed_suspicious(self):
338-
m = Module()
339-
with m.If(0):
340-
pass
341-
with self.assertWarnsRegex(SyntaxWarning,
342-
(r"^Signed values in If\/Elif conditions usually result from inverting Python "
339+
def test_Elif_signed_suspicious(self):
340+
m = Module()
341+
with m.If(0):
342+
pass
343+
with self.assertWarnsRegex(SyntaxWarning,
344+
r"^Signed values in If\/Elif conditions usually result from inverting Python "
343345
r"booleans with ~, which leads to unexpected results\. Replace `~flag` with "
344346
r"`not flag`\. \(If this is a false positive, silence this warning with "
345-
r"`m\.If\(x\)` → `m\.If\(x\.bool\(\)\)`\.\)$")):
346-
with m.Elif(~True):
347-
pass
347+
r"`m\.If\(x\)` → `m\.If\(x\.bool\(\)\)`\.\)$"):
348+
with m.Elif(~True):
349+
pass
348350

349351
def test_if_If_Elif_Else(self):
350352
m = Module()

0 commit comments

Comments
 (0)