https://github.com/python/cpython/commit/7668006378b8cc80dc6e9b8a04412a953551850a
commit: 7668006378b8cc80dc6e9b8a04412a953551850a
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: hugovk <[email protected]>
date: 2026-11-01T23:51:01+02:00
summary:

[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 410fdda081ba1ca95be0c2ed85c9a7ab2f560604)

Co-authored-by: Olayinka Vaughan <[email protected]>

files:
M Doc/howto/annotations.rst

diff --git a/Doc/howto/annotations.rst b/Doc/howto/annotations.rst
index fcc4fe838c783ba..e9d490a078c57ef 100644
--- a/Doc/howto/annotations.rst
+++ b/Doc/howto/annotations.rst
@@ -243,10 +243,22 @@ If you use a class with a custom metaclass and access 
``__annotations__``
 on the class, you may observe unexpected behavior; see
 :pep:`749 <749#pep749-metaclasses>` for some examples. You can avoid these
 quirks by using :func:`annotationlib.get_annotations` on Python 3.14+ or
-:func:`inspect.get_annotations` on Python 3.10+. On earlier versions of
-Python, you can avoid these bugs by accessing the annotations from the
-class's :attr:`~type.__dict__`
-(for example, ``cls.__dict__.get('__annotations__', None)``).
+:func:`inspect.get_annotations` on Python 3.10+.
+
+.. note::
+
+   On Python 3.14 and later, annotations are evaluated lazily (see
+   :pep:`649` and :pep:`749`), and how the annotations dict is stored in
+   the class's :attr:`~type.__dict__` is a private implementation detail.
+   Reading ``cls.__dict__.get('__annotations__', None)`` may therefore
+   return ``None`` even when the class has annotations. On these versions,
+   always use :func:`annotationlib.get_annotations` (or
+   :func:`inspect.get_annotations`) rather than accessing
+   :attr:`~type.__dict__` directly.
+
+Only on Python 3.9 and earlier, where neither function is available, can you
+avoid these bugs by reading the annotations from the class's
+:attr:`~type.__dict__` (for example, ``cls.__dict__.get('__annotations__', 
None)``).
 
 In some versions of Python, instances of classes may have an 
``__annotations__``
 attribute. However, this is not supported functionality. If you need the

_______________________________________________
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]

Reply via email to