.. function:: get_annotate_function(obj)
Retrieve the :term:`annotate function` for *obj*. Return :const:`!None`
- if *obj* does not have an annotate function.
+ if *obj* does not have an annotate function. *obj* may be a class, function,
+ module, or a namespace dictionary for a class. The last case is useful during
+ class creation, e.g. in the ``__new__`` method of a metaclass.
This is usually equivalent to accessing the :attr:`~object.__annotate__`
- attribute of *obj*, but direct access to the attribute may return the wrong
- object in certain situations involving metaclasses. This function should be
- used instead of accessing the attribute directly.
+ attribute of *obj*, but access through this public function is preferred.
.. versionadded:: 3.14
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__and__));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__anext__));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__annotate__));
+ _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__annotate_func__));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__annotations__));
+ _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__annotations_cache__));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__args__));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__await__));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__bases__));
STRUCT_FOR_ID(__and__)
STRUCT_FOR_ID(__anext__)
STRUCT_FOR_ID(__annotate__)
+ STRUCT_FOR_ID(__annotate_func__)
STRUCT_FOR_ID(__annotations__)
+ STRUCT_FOR_ID(__annotations_cache__)
STRUCT_FOR_ID(__args__)
STRUCT_FOR_ID(__await__)
STRUCT_FOR_ID(__bases__)
Python 3.14a6 3619 (Renumber RESUME opcode from 149 to 128)
Python 3.14a6 3620 (Optimize bytecode for all/any/tuple called on a genexp)
Python 3.14a7 3621 (Optimize LOAD_FAST opcodes into LOAD_FAST_BORROW)
+ Python 3.14a7 3622 (Store annotations in different class dict keys)
Python 3.15 will start with 3650
*/
-#define PYC_MAGIC_NUMBER 3621
+#define PYC_MAGIC_NUMBER 3622
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
INIT_ID(__and__), \
INIT_ID(__anext__), \
INIT_ID(__annotate__), \
+ INIT_ID(__annotate_func__), \
INIT_ID(__annotations__), \
+ INIT_ID(__annotations_cache__), \
INIT_ID(__args__), \
INIT_ID(__await__), \
INIT_ID(__bases__), \
_PyUnicode_InternStatic(interp, &string);
assert(_PyUnicode_CheckConsistency(string, 1));
assert(PyUnicode_GET_LENGTH(string) != 1);
+ string = &_Py_ID(__annotate_func__);
+ _PyUnicode_InternStatic(interp, &string);
+ assert(_PyUnicode_CheckConsistency(string, 1));
+ assert(PyUnicode_GET_LENGTH(string) != 1);
string = &_Py_ID(__annotations__);
_PyUnicode_InternStatic(interp, &string);
assert(_PyUnicode_CheckConsistency(string, 1));
assert(PyUnicode_GET_LENGTH(string) != 1);
+ string = &_Py_ID(__annotations_cache__);
+ _PyUnicode_InternStatic(interp, &string);
+ assert(_PyUnicode_CheckConsistency(string, 1));
+ assert(PyUnicode_GET_LENGTH(string) != 1);
string = &_Py_ID(__args__);
_PyUnicode_InternStatic(interp, &string);
assert(_PyUnicode_CheckConsistency(string, 1));
raise ValueError(f"Invalid format: {format!r}")
-# We use the descriptors from builtins.type instead of accessing
-# .__annotations__ and .__annotate__ directly on class objects, because
-# otherwise we could get wrong results in some cases involving metaclasses.
-# See PEP 749.
-_BASE_GET_ANNOTATE = type.__dict__["__annotate__"].__get__
-_BASE_GET_ANNOTATIONS = type.__dict__["__annotations__"].__get__
-
-
def get_annotate_function(obj):
"""Get the __annotate__ function for an object.
Returns the __annotate__ function or None.
"""
- if isinstance(obj, type):
+ if isinstance(obj, dict):
try:
- return _BASE_GET_ANNOTATE(obj)
- except AttributeError:
- # AttributeError is raised for static types.
- return None
+ return obj["__annotate__"]
+ except KeyError:
+ return obj.get("__annotate_func__", None)
return getattr(obj, "__annotate__", None)
def _get_dunder_annotations(obj):
if isinstance(obj, type):
try:
- ann = _BASE_GET_ANNOTATIONS(obj)
+ ann = obj.__annotations__
except AttributeError:
# For static types, the descriptor raises AttributeError.
return {}
'__date__', '__doc__', '__file__', '__spec__',
'__loader__', '__module__', '__name__', '__package__',
'__path__', '__qualname__', '__slots__', '__version__',
- '__static_attributes__', '__firstlineno__'}:
+ '__static_attributes__', '__firstlineno__',
+ '__annotate_func__', '__annotations_cache__'}:
return 0
# Private names are hidden, but special names are displayed.
if name.startswith('__') and name.endswith('__'): return 1
x = ast.arguments()
self.assertEqual(x._fields, ('posonlyargs', 'args', 'vararg', 'kwonlyargs',
'kw_defaults', 'kwarg', 'defaults'))
- self.assertEqual(x.__annotations__, {
+ self.assertEqual(ast.arguments.__annotations__, {
'posonlyargs': list[ast.arg],
'args': list[ast.arg],
'vararg': ast.arg | None,
| __weakref__%s
class B(builtins.object)
- | Methods defined here:
- |
- | __annotate__(format, /)
- |
- | ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__%s
list of weak references to the object
class B(builtins.object)
- Methods defined here:
- __annotate__(format, /)
- ----------------------------------------------------------------------
Data descriptors defined here:
__dict__
dictionary for instance variables
import types
import unittest
from test.support import run_code, check_syntax_error, cpython_only
+from test.test_inspect import inspect_stringized_annotations
class TypeAnnotationTests(unittest.TestCase):
def test_lazy_create_annotations(self):
# type objects lazy create their __annotations__ dict on demand.
- # the annotations dict is stored in type.__dict__.
+ # the annotations dict is stored in type.__dict__ (as __annotations_cache__).
# a freshly created type shouldn't have an annotations dict yet.
foo = type("Foo", (), {})
for i in range(3):
- self.assertFalse("__annotations__" in foo.__dict__)
+ self.assertFalse("__annotations_cache__" in foo.__dict__)
d = foo.__annotations__
- self.assertTrue("__annotations__" in foo.__dict__)
+ self.assertTrue("__annotations_cache__" in foo.__dict__)
self.assertEqual(foo.__annotations__, d)
- self.assertEqual(foo.__dict__['__annotations__'], d)
+ self.assertEqual(foo.__dict__['__annotations_cache__'], d)
del foo.__annotations__
def test_setting_annotations(self):
foo = type("Foo", (), {})
for i in range(3):
- self.assertFalse("__annotations__" in foo.__dict__)
+ self.assertFalse("__annotations_cache__" in foo.__dict__)
d = {'a': int}
foo.__annotations__ = d
- self.assertTrue("__annotations__" in foo.__dict__)
+ self.assertTrue("__annotations_cache__" in foo.__dict__)
self.assertEqual(foo.__annotations__, d)
- self.assertEqual(foo.__dict__['__annotations__'], d)
+ self.assertEqual(foo.__dict__['__annotations_cache__'], d)
del foo.__annotations__
def test_annotations_getset_raises(self):
a:int=3
b:str=4
self.assertEqual(C.__annotations__, {"a": int, "b": str})
- self.assertTrue("__annotations__" in C.__dict__)
+ self.assertTrue("__annotations_cache__" in C.__dict__)
del C.__annotations__
- self.assertFalse("__annotations__" in C.__dict__)
+ self.assertFalse("__annotations_cache__" in C.__dict__)
+
+ def test_pep563_annotations(self):
+ isa = inspect_stringized_annotations
+ self.assertEqual(
+ isa.__annotations__, {"a": "int", "b": "str"},
+ )
+ self.assertEqual(
+ isa.MyClass.__annotations__, {"a": "int", "b": "str"},
+ )
+
+ def test_explicitly_set_annotations(self):
+ class C:
+ __annotations__ = {"what": int}
+ self.assertEqual(C.__annotations__, {"what": int})
+
+ def test_explicitly_set_annotate(self):
+ class C:
+ __annotate__ = lambda format: {"what": int}
+ self.assertEqual(C.__annotations__, {"what": int})
+ self.assertIsInstance(C.__annotate__, types.FunctionType)
+ self.assertEqual(C.__annotate__(annotationlib.Format.VALUE), {"what": int})
def test_del_annotations_and_annotate(self):
# gh-132285
acceptable_extra_attrs = {
'_is_protocol', '_is_runtime_protocol', '__parameters__',
'__init__', '__annotations__', '__subclasshook__', '__annotate__',
+ '__annotations_cache__', '__annotate_func__',
}
self.assertLessEqual(vars(NonP).keys(), vars(C).keys() | acceptable_extra_attrs)
self.assertLessEqual(
'__init__', '__module__', '__new__', '__slots__',
'__subclasshook__', '__weakref__', '__class_getitem__',
'__match_args__', '__static_attributes__', '__firstlineno__',
- '__annotate__',
+ '__annotate__', '__annotate_func__', '__annotations_cache__',
})
# These special attributes will be not collected as protocol members.
'_fields', '_field_defaults',
'_make', '_replace', '_asdict', '_source'})
-_special = frozenset({'__module__', '__name__', '__annotations__', '__annotate__'})
+_special = frozenset({'__module__', '__name__', '__annotations__', '__annotate__',
+ '__annotate_func__', '__annotations_cache__'})
class NamedTupleMeta(type):
types = ns["__annotations__"]
field_names = list(types)
annotate = _make_eager_annotate(types)
- elif "__annotate__" in ns:
- original_annotate = ns["__annotate__"]
+ elif (original_annotate := _lazy_annotationlib.get_annotate_function(ns)) is not None:
types = _lazy_annotationlib.call_annotate_function(
original_annotate, _lazy_annotationlib.Format.FORWARDREF)
field_names = list(types)
if "__annotations__" in ns:
own_annotate = None
own_annotations = ns["__annotations__"]
- elif "__annotate__" in ns:
- own_annotate = ns["__annotate__"]
+ elif (own_annotate := _lazy_annotationlib.get_annotate_function(ns)) is not None:
own_annotations = _lazy_annotationlib.call_annotate_function(
own_annotate, _lazy_annotationlib.Format.FORWARDREF, owner=tp_dict
)
--- /dev/null
+The internal storage for annotations and annotate functions on classes now
+uses different keys in the class dictionary. This eliminates various edge
+cases where access to the ``__annotate__`` and ``__annotations__``
+attributes would behave unpredictably.
PyObject *annotate;
PyObject *dict = PyType_GetDict(type);
+ // First try __annotate__, in case that's been set explicitly
if (PyDict_GetItemRef(dict, &_Py_ID(__annotate__), &annotate) < 0) {
Py_DECREF(dict);
return NULL;
}
+ if (!annotate) {
+ if (PyDict_GetItemRef(dict, &_Py_ID(__annotate_func__), &annotate) < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
+ }
if (annotate) {
descrgetfunc get = Py_TYPE(annotate)->tp_descr_get;
if (get) {
}
else {
annotate = Py_None;
- int result = PyDict_SetItem(dict, &_Py_ID(__annotate__), annotate);
+ int result = PyDict_SetItem(dict, &_Py_ID(__annotate_func__), annotate);
if (result < 0) {
Py_DECREF(dict);
return NULL;
PyObject *dict = PyType_GetDict(type);
assert(PyDict_Check(dict));
- int result = PyDict_SetItem(dict, &_Py_ID(__annotate__), value);
+ int result = PyDict_SetItem(dict, &_Py_ID(__annotate_func__), value);
if (result < 0) {
Py_DECREF(dict);
return -1;
}
if (!Py_IsNone(value)) {
- if (PyDict_Pop(dict, &_Py_ID(__annotations__), NULL) == -1) {
+ if (PyDict_Pop(dict, &_Py_ID(__annotations_cache__), NULL) == -1) {
Py_DECREF(dict);
PyType_Modified(type);
return -1;
PyObject *annotations;
PyObject *dict = PyType_GetDict(type);
+ // First try __annotations__ (e.g. for "from __future__ import annotations")
if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) < 0) {
Py_DECREF(dict);
return NULL;
}
+ if (!annotations) {
+ if (PyDict_GetItemRef(dict, &_Py_ID(__annotations_cache__), &annotations) < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
+ }
+
if (annotations) {
descrgetfunc get = Py_TYPE(annotations)->tp_descr_get;
if (get) {
}
}
else {
- PyObject *annotate = type_get_annotate(tp, NULL);
+ PyObject *annotate = PyObject_GetAttrString((PyObject *)type, "__annotate__");
if (annotate == NULL) {
Py_DECREF(dict);
return NULL;
Py_DECREF(annotate);
if (annotations) {
int result = PyDict_SetItem(
- dict, &_Py_ID(__annotations__), annotations);
+ dict, &_Py_ID(__annotations_cache__), annotations);
if (result) {
Py_CLEAR(annotations);
} else {
PyObject *dict = PyType_GetDict(type);
if (value != NULL) {
/* set */
- result = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
+ result = PyDict_SetItem(dict, &_Py_ID(__annotations_cache__), value);
} else {
/* delete */
- result = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
+ result = PyDict_Pop(dict, &_Py_ID(__annotations_cache__), NULL);
if (result == 0) {
PyErr_SetString(PyExc_AttributeError, "__annotations__");
Py_DECREF(dict);
Py_DECREF(dict);
return -1;
} else { // result can be 0 or 1
+ if (PyDict_Pop(dict, &_Py_ID(__annotate_func__), NULL) < 0) {
+ PyType_Modified(type);
+ Py_DECREF(dict);
+ return -1;
+ }
if (PyDict_Pop(dict, &_Py_ID(__annotate__), NULL) < 0) {
PyType_Modified(type);
Py_DECREF(dict);
Py_DECREF(conditional_annotation_indices);
RETURN_IF_ERROR(codegen_leave_annotations_scope(c, loc));
- RETURN_IF_ERROR(codegen_nameop(c, loc, &_Py_ID(__annotate__), Store));
+ RETURN_IF_ERROR(codegen_nameop(
+ c, loc,
+ ste->ste_type == ClassBlock ? &_Py_ID(__annotate_func__) : &_Py_ID(__annotate__),
+ Store));
return SUCCESS;
error: