https://github.com/python/cpython/commit/7d3fca4e047f5b0a9beccc447c03cd6b26259b3b
commit: 7d3fca4e047f5b0a9beccc447c03cd6b26259b3b
branch: main
author: Bhuvansh <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-06T19:34:54-07:00
summary:

gh-153354: Fix bool conversion in generated __annotate__ (#153360)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-07-09-13-28-27.gh-issue-153354.It4Jj_.rst
M Lib/test/test_annotationlib.py
M Python/codegen.c

diff --git a/Lib/test/test_annotationlib.py b/Lib/test/test_annotationlib.py
index a7d3bca7402bf2..b5388f3f30aee0 100644
--- a/Lib/test/test_annotationlib.py
+++ b/Lib/test/test_annotationlib.py
@@ -1720,6 +1720,37 @@ def annotate(format, /):
             with self.assertRaises(DemoException):
                 annotationlib.call_annotate_function(annotate, format=fmt)
 
+    def test_generated_annotate_non_bool_compare_result(self):
+        class NonBool(int):
+            def __gt__(self, other):
+                return None
+
+        # Function __annotate__
+        def func(x: int):
+            pass
+
+        self.assertEqual(
+            func.__annotate__(NonBool(1)),
+            {"x": int},
+        )
+
+        # Class __annotate__
+        class C:
+            y: int
+
+        self.assertEqual(
+            C.__annotate__(NonBool(1)),
+            {"y": int},
+        )
+
+        # Module __annotate__
+        ns = {}
+        exec("z: int", ns)
+
+        self.assertEqual(
+            ns["__annotate__"](NonBool(1)),
+            {"z": int},
+        )
 
 class MetaclassTests(unittest.TestCase):
     def test_annotated_meta(self):
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-09-13-28-27.gh-issue-153354.It4Jj_.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-09-13-28-27.gh-issue-153354.It4Jj_.rst
new file mode 100644
index 00000000000000..4cfd4302b3c34d
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-09-13-28-27.gh-issue-153354.It4Jj_.rst
@@ -0,0 +1,2 @@
+Fix a crash in compiler-generated ``__annotate__`` functions when the
+annotation format comparison returned a non-boolean result.
diff --git a/Python/codegen.c b/Python/codegen.c
index 7143d9abc4255e..840dd3103a81c5 100644
--- a/Python/codegen.c
+++ b/Python/codegen.c
@@ -729,6 +729,7 @@ codegen_emit_annotations_prologue(compiler *c, location loc)
     ADDOP_I(c, loc, LOAD_FAST, 0);
     ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals);
     ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
+    ADDOP(c, loc, TO_BOOL);
     NEW_JUMP_TARGET_LABEL(c, body);
     ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);
     ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, CONSTANT_NOTIMPLEMENTEDERROR);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to