https://github.com/python/cpython/commit/254110356d17204d2aebb9e74b6074cc6ef6382e commit: 254110356d17204d2aebb9e74b6074cc6ef6382e branch: main author: sobolevn <m...@sobolevn.me> committer: JelleZijlstra <jelle.zijls...@gmail.com> date: 2025-04-09T10:36:08-07:00 summary:
gh-132285: Fix that `__annotate__` is not deleted when `__annotations__` is deleted (#132286) files: A Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-12-37-31.gh-issue-132286.1ZdsOa.rst M Lib/test/test_type_annotations.py M Objects/typeobject.c diff --git a/Lib/test/test_type_annotations.py b/Lib/test/test_type_annotations.py index 9c70d27d29e89c..29e2c7a0cd837e 100644 --- a/Lib/test/test_type_annotations.py +++ b/Lib/test/test_type_annotations.py @@ -57,6 +57,26 @@ class C: del C.__annotations__ self.assertFalse("__annotations__" in C.__dict__) + def test_del_annotations_and_annotate(self): + # gh-132285 + called = False + class A: + def __annotate__(format): + nonlocal called + called = True + return {'a': int} + + self.assertEqual(A.__annotations__, {'a': int}) + self.assertTrue(called) + self.assertTrue(A.__annotate__) + + del A.__annotations__ + called = False + + self.assertEqual(A.__annotations__, {}) + self.assertFalse(called) + self.assertIs(A.__annotate__, None) + def test_descriptor_still_works(self): class C: def __init__(self, name=None, bases=None, d=None): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-12-37-31.gh-issue-132286.1ZdsOa.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-12-37-31.gh-issue-132286.1ZdsOa.rst new file mode 100644 index 00000000000000..82dcbd3bc101de --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-09-12-37-31.gh-issue-132286.1ZdsOa.rst @@ -0,0 +1,2 @@ +Fix that :attr:`type.__annotate__` was not deleted, when +:attr:`type.__annotations__` was deleted. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index b92eaefc90d0af..75c23ddd91b1a1 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2066,8 +2066,7 @@ type_set_annotations(PyObject *tp, PyObject *value, void *Py_UNUSED(closure)) if (result < 0) { Py_DECREF(dict); return -1; - } - else if (result == 0) { + } else { // result can be 0 or 1 if (PyDict_Pop(dict, &_Py_ID(__annotate__), NULL) < 0) { PyType_Modified(type); Py_DECREF(dict); _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com