8000 gh-132285: Fix that `__annotate__` is not deleted when `__annotations__` is deleted by sobolevn · Pull Request #132286 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132285: Fix that __annotate__ is not deleted when __annotations__ is deleted #132286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,24 @@ def f(x: int):
{"x": "int"},
)

def test_del_annotations(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)

del A.__annotations__
called = False

self.assertEqual(A.__annotations__, {})
self.assertFalse(called)

def test_non_dict_annotations(self):
class WeirdAnnotations:
@property
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix that :attr:`type.__annotate__` was not deleted, when
:attr:`type.__annotations__` was deleted.
3 changes: 1 addition & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
0