Skip to content

Commit de184ba

Browse files
committed
gh-153354: Fix bool conversion in generated __annotate__
1 parent 9fb713f commit de184ba

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lib/test/test_annotationlib.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,37 @@ def annotate(format, /):
17131713
with self.assertRaises(DemoException):
17141714
annotationlib.call_annotate_function(annotate, format=fmt)
17151715

1716+
def test_generated_annotate_non_bool_compare_result(self):
1717+
class NonBool:
1718+
def __gt__(self, other):
1719+
return None
1720+
1721+
# Function __annotate__
1722+
def func(x: int):
1723+
pass
1724+
1725+
self.assertEqual(
1726+
func.__annotate__(NonBool()),
1727+
{"x": int},
1728+
)
1729+
1730+
# Class __annotate__
1731+
class C:
1732+
y: int
1733+
1734+
self.assertEqual(
1735+
C.__annotate__(NonBool()),
1736+
{"y": int},
1737+
)
1738+
1739+
# Module __annotate__
1740+
ns = {}
1741+
exec("z: int", ns)
1742+
1743+
self.assertEqual(
1744+
ns["__annotate__"](NonBool()),
1745+
{"z": int},
1746+
)
17161747

17171748
class MetaclassTests(unittest.TestCase):
17181749
def test_annotated_meta(self):

Python/codegen.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ codegen_setup_annotations_scope(compiler *c, location loc,
724724
ADDOP_I(c, loc, LOAD_FAST, 0);
725725
ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals);
726726
ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
727+
ADDOP(c, loc, TO_BOOL);
727728
NEW_JUMP_TARGET_LABEL(c, body);
728729
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);
729730
ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, CONSTANT_NOTIMPLEMENTEDERROR);

0 commit comments

Comments
 (0)