Skip to content

Commit 7668006

Browse files
[3.15] gh-139140: Document that reading __annotations__ via __dict__ is unre… (GH-151750) (#161051)
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 2f87f80 commit 7668006

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

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

0 commit comments

Comments
 (0)