https://github.com/python/cpython/commit/cc81707e406c49c63afc18048e1a221d796ce638
commit: cc81707e406c49c63afc18048e1a221d796ce638
branch: main
author: Nybblista <[email protected]>
committer: vstinner <[email protected]>
date: 2026-02-10T14:38:24Z
summary:
gh-144629: Add test for the PyFunction_GetAnnotations() function (#144630)
files:
M Lib/test/test_capi/test_function.py
M Modules/_testcapi/function.c
diff --git a/Lib/test/test_capi/test_function.py
b/Lib/test/test_capi/test_function.py
index 9dca377e28ba42..c1a278e5d4da91 100644
--- a/Lib/test/test_capi/test_function.py
+++ b/Lib/test/test_capi/test_function.py
@@ -307,10 +307,27 @@ def function_without_closure(): ...
_testcapi.function_get_closure(function_without_closure), (1, 2))
self.assertEqual(function_without_closure.__closure__, (1, 2))
+ def test_function_get_annotations(self):
+ # Test PyFunction_GetAnnotations()
+ def normal():
+ pass
+
+ def annofn(arg: int) -> str:
+ return f'arg = {arg}'
+
+ annotations = _testcapi.function_get_annotations(normal)
+ self.assertIsNone(annotations)
+
+ annotations = _testcapi.function_get_annotations(annofn)
+ self.assertIsInstance(annotations, dict)
+ self.assertEqual(annotations, annofn.__annotations__)
+
+ with self.assertRaises(SystemError):
+ _testcapi.function_get_annotations(None)
+
# TODO: test PyFunction_New()
# TODO: test PyFunction_NewWithQualName()
# TODO: test PyFunction_SetVectorcall()
- # TODO: test PyFunction_GetAnnotations()
# TODO: test PyFunction_SetAnnotations()
# TODO: test PyClassMethod_New()
# TODO: test PyStaticMethod_New()
diff --git a/Modules/_testcapi/function.c b/Modules/_testcapi/function.c
index ec1ba508df2ce9..40767adbd3f14a 100644
--- a/Modules/_testcapi/function.c
+++ b/Modules/_testcapi/function.c
@@ -123,6 +123,13 @@ function_set_closure(PyObject *self, PyObject *args)
}
+static PyObject *
+function_get_annotations(PyObject *self, PyObject *func)
+{
+ return Py_XNewRef(PyFunction_GetAnnotations(func));
+}
+
+
static PyMethodDef test_methods[] = {
{"function_get_code", function_get_code, METH_O, NULL},
{"function_get_globals", function_get_globals, METH_O, NULL},
@@ -133,6 +140,7 @@ static PyMethodDef test_methods[] = {
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
{"function_get_closure", function_get_closure, METH_O, NULL},
{"function_set_closure", function_set_closure, METH_VARARGS, NULL},
+ {"function_get_annotations", function_get_annotations, METH_O, NULL},
{NULL},
};
_______________________________________________
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]