8000 gh-132285: Fix that `__annotate__` is not deleted when `__annotations… · python/cpython@2541103 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2541103

Browse files
authored
gh-132285: Fix that __annotate__ is not deleted when __annotations__ is deleted (#132286)
1 parent 1f5682f commit 2541103

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Lib/test/test_type_annotations.py

Lines changed: 20 additions & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ class C:
5757
del C.__annotations__
5858
self.assertFalse("__annotations__" in C.__dict__)
5959

60+
def test_del_annotations_and_annotate(self):
61+
# gh-132285
62+
called = False
63+
class A:
64+
def __annotate__(format):
65+
nonlocal called
66+
called = True
67+
return {'a': int}
68+
69+
self.assertEqual(A.__annotations__, {'a': int})
70+
self.assertTrue(called)
71+
self.assertTrue(A.__annotate__)
72+
73+
del A.__annotations__
74+
called = False
75+
76+
self.assertEqual(A.__annotations__, {})
77+
self.assertFalse(called)
78+
self.assertIs(A.__annotate__, None)
79+
6080
def test_descriptor_still_works(self):
6181
class C:
6282
def __init__(self, name=None, bases=None, d=None):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix that :attr:`type.__annotate__` was not deleted, when
2+
:attr:`type.__annotations__` was deleted.

Objects/typeobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,8 +2066,7 @@ type_set_annotations(PyObject *tp, PyObject *value, void *Py_UNUSED(closure))
20662066
if (result < 0) {
20672067
Py_DECREF(dict);
20682068
return -1;
2069-
}
2070-
else if (result == 0) {
2069+
} else { // result can be 0 or 1
20712070
if (PyDict_Pop(dict, &_Py_ID(__annotate__), NULL) < 0) {
20722071
PyType_Modified(type);
20732072
Py_DECREF(dict);

0 commit comments

Comments
 (0)
0