Skip to content

Commit 361e81b

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

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lib/test/test_annotationlib.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,18 @@ 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+
def func(x: int):
1722+
pass
1723+
1724+
self.assertEqual(
1725+
func.__annotate__(NonBool()),
1726+
{"x": int},
1727+
)
17161728

17171729
class MetaclassTests(unittest.TestCase):
17181730
def test_annotated_meta(self):

Python/codegen.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,8 @@ codegen_setup_annotations_scope(compiler *c, location loc,
723723
_Py_DECLARE_STR(format, ".format");
724724
ADDOP_I(c, loc, LOAD_FAST, 0);
725725
ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals);
726-
ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
726+
ADDOP_I(c, loc, COMPARE_OP,
727+
(Py_GT << 5) | compare_masks[Py_GT] | 16);
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)