Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7291eab

Browse files
JelleZijlstraemmatypingwillingc
authored
gh-119180: Updates to PEP 649/749 docs (#134640)
- Mention (again) that `type.__annotations__` is unsafe. It is now safe when using only classes defined under PEP 649 semantics, but not with classes defined using `from __future__ import annotations`. - Mention that annotations on instances no longer work. There was already an issue about this. - Mention the general changes in the "Porting to Python 3.14" section. - `annotationlib` was proposed by PEP-749, not PEP-649. Co-authored-by: Emma Smith <[email protected]> Co-authored-by: Carol Willing <[email protected]>
1 parent 806107d commit 7291eab

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Doc/reference/datamodel.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,10 +1228,22 @@ Special attributes
12281228
:attr:`__annotations__ attributes <object.__annotations__>`.
12291229

12301230
For best practices on working with :attr:`~object.__annotations__`,
1231-
please see :mod:`annotationlib`. Where possible, use
1231+
please see :mod:`annotationlib`. Use
12321232
:func:`annotationlib.get_annotations` instead of accessing this
12331233
attribute directly.
12341234

1235+
.. warning::
1236+
1237+
Accessing the :attr:`!__annotations__` attribute directly
1238+
on a class object may return annotations for the wrong class, specifically
1239+
in certain cases where the class, its base class, or a metaclass
1240+
is defined under ``from __future__ import annotations``.
1241+
See :pep:`749 <749#pep749-metaclasses>` for details.
1242+
1243+
This attribute does not exist on certain builtin classes. On
1244+
user-defined classes without ``__annotations__``, it is an
1245+
empty dictionary.
1246+
12351247
.. versionchanged:: 3.14
12361248
Annotations are now :ref:`lazily evaluated <lazy-evaluation>`.
12371249
See :pep:`649`.

Doc/whatsnew/3.14.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ deferred evaluation of annotations (:pep:`649`),
7474
and a new type of interpreter that uses tail calls.
7575

7676
The library changes include the addition of a new :mod:`!annotationlib` module
77-
for introspecting and wrapping annotations (:pep:`649`),
77+
for introspecting and wrapping annotations (:pep:`749`),
7878
a new :mod:`!compression.zstd` module for Zstandard support (:pep:`784`),
7979
plus syntax highlighting in the REPL,
8080
as well as the usual deprecations and removals,
@@ -444,6 +444,10 @@ In particular, do not read annotations directly from the namespace dictionary
444444
attribute of type objects. Use :func:`annotationlib.get_annotate_from_class_namespace`
445445
during class construction and :func:`annotationlib.get_annotations` afterwards.
446446

447+
In previous releases, it was sometimes possible to access class annotations from
448+
an instance of an annotated class. This behavior was undocumented and accidental,
449+
and will no longer work in Python 3.14.
450+
447451
``from __future__ import annotations``
448452
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
449453

@@ -2501,6 +2505,11 @@ Changes in the Python API
25012505
See :ref:`above <whatsnew314-typing-union>` for more details.
25022506
(Contributed by Jelle Zijlstra in :gh:`105499`.)
25032507

2508+
* The runtime behavior of annotations has changed in various ways; see
2509+
:ref:`above <whatsnew314-pep649>` for details. While most code that interacts
2510+
with annotations should continue to work, some undocumented details may behave
2511+
differently.
2512+
25042513

25052514
Build changes
25062515
=============

0 commit comments

Comments
 (0)