8000 gh-119180: PEP 649: Add __annotate__ attributes by JelleZijlstra · Pull Request #119209 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-119180: PEP 649: Add __annotate__ attributes #119209

8000
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 11 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix refleaks
  • Loading branch information
JelleZijlstra committed May 20, 2024
commit c822ffab59966c02b5acb12c6991b778d03dfade
4 changes: 2 additions & 2 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_match(self):

class AnnotateTests(unittest.TestCase):
"""See PEP 649."""
def test_manual_annotate_function(self):
def test_manual_annotate(self):
def f():
pass
mod = types.ModuleType("mod")
Expand All @@ -232,7 +232,7 @@ def check_annotations(self, f):
self.assertEqual(f.__annotations__, {})
self.assertIs(f.__annotate__, None)

with self.assertRaises(TypeError):
with self.assertRaisesRegex(TypeError, "__annotate__ must be callable or None"):
f.__annotate__ = 42
f.__annotate__ = lambda: 42
with self.assertRaisesRegex(TypeError, r"takes 0 positional arguments but 1 was given"):
Expand Down
2 changes: 1 addition & 1 deletion Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func_set_annotate(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored
}
else {
PyErr_SetString(PyExc_TypeError,
"__annotate__ must be set to a callable object or None");
"__annotate__ must be callable or None");
return -1;
}
}
Expand Down
4 changes: 4 additions & 0 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1220,10 +1220,13 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
PyObject *one = _PyLong_GetOne();
annotations = _PyObject_CallOneArg(annotate, one);
if (annotations == NULL) {
Py_DECREF(annotate);
Py_DECREF(dict);
return NULL;
}
if (!PyDict_Check(annotations)) {
PyErr_SetString(PyExc_TypeError, "__annotate__ returned a non-dict");
Py_DECREF(annotate);
Py_DECREF(annotations);
Py_DECREF(dict);
return NULL;
Expand All @@ -1232,6 +1235,7 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
else {
annotations = PyDict_New();
}
Py_XDECREF(annotate);
if (annotations) {
int result = PyDict_SetItem(
dict, &_Py_ID(__annotations__), annotations);
Expand Down
3 changes: 3 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1764,17 +1764,20 @@ type_get_annotations(PyTypeObject *type, void *context)
PyObject *one = _PyLong_GetOne();
annotations = _PyObject_CallOneArg(annotate, one);
if (annotations == NULL) {
Py_DECREF(annotate);
return NULL;
}
if (!PyDict_Check(annotations)) {
PyErr_SetString(PyExc_TypeError, "__annotate__ returned a non-dict");
Py_DECREF(annotations);
Py_DECREF(annotate);
return NULL;
}
}
else {
annotations = PyDict_New();
}
Py_DECREF(annotate);
if (annotations) {
int result = PyDict_SetItem(
dict, &_Py_ID(__annotations__), annotations);
Expand Down
0