Skip to content

Commit 2ef0a56

Browse files
[3.14] gh-139140: Document that reading __annotations__ via __dict__ is unre… (GH-151750) (#161050)
gh-139140: Document that reading __annotations__ via __dict__ is unre… (GH-151750) (cherry picked from commit 410fdda) Co-authored-by: Olayinka Vaughan <114308865+Builder106@users.noreply.github.com>
1 parent 3630594 commit 2ef0a56

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

‎Doc/howto/annotations.rst‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,22 @@ If you use a class with a custom metaclass and access ``__annotations__``
245245
on the class, you may observe unexpected behavior; see
246246
:pep:`749 <749#pep749-metaclasses>` for some examples. You can avoid these
247247
quirks by using :func:`annotationlib.get_annotations` on Python 3.14+ or
248-
:func:`inspect.get_annotations` on Python 3.10+. On earlier versions of
249-
Python, you can avoid these bugs by accessing the annotations from the
250-
class's :attr:`~type.__dict__`
251-
(for example, ``cls.__dict__.get('__annotations__', None)``).
248+
:func:`inspect.get_annotations` on Python 3.10+.
249+
250+
.. note::
251+
252+
On Python 3.14 and later, annotations are evaluated lazily (see
253+
:pep:`649` and :pep:`749`), and how the annotations dict is stored in
254+
the class's :attr:`~type.__dict__` is a private implementation detail.
255+
Reading ``cls.__dict__.get('__annotations__', None)`` may therefore
256+
return ``None`` even when the class has annotations. On these versions,
257+
always use :func:`annotationlib.get_annotations` (or
258+
:func:`inspect.get_annotations`) rather than accessing
259+
:attr:`~type.__dict__` directly.
260+
261+
Only on Python 3.9 and earlier, where neither function is available, can you
262+
avoid these bugs by reading the annotations from the class's
263+
:attr:`~type.__dict__` (for example, ``cls.__dict__.get('__annotations__', None)``).
252264

253265
In some versions of Python, instances of classes may have an ``__annotations__``
254266
attribute. However, this is not supported functionality. If you need the

0 commit comments

Comments
 (0)