@@ -70,6 +70,91 @@ Summary -- Release highlights
70
70
New Features
71
71
============
72
72
73
+ .. _whatsnew-314-pep649 :
74
+
75
+ PEP 649: Deferred Evaluation of Annotations
76
+ -------------------------------------------
77
+
78
+ The :term: `annotations <annotation> ` on functions, classes, and modules are no
79
+ longer evaluated eagerly. Instead, annotations are stored in special-purpose
80
+ :term: `annotate functions <annotate function> ` and evaluated only when
81
+ necessary. This is specified in :pep: `649 ` and :pep: `749 `.
82
+
83
+ This change is designed to make annotations in Python more performant and more
84
+ usable in most circumstances. The runtime cost for defining annotations is
85
+ minimized, but it remains possible to introspect annotations at runtime.
86
+ It is usually no longer necessary to enclose annotations in strings if they
87
+ contain forward references.
88
+
89
+ The new :mod: `annotationlib ` module provides tools for inspecting deferred
90
+ annotations. Annotations may be evaluated in the :attr: `~annotationlib.Format.VALUE `
91
+ format (which evaluates annotations to runtime values, similar to the behavior in
92
+ earlier Python versions), the :attr: `~annotationlib.Format.FORWARDREF ` format
93
+ (which replaces undefined names with special markers), and the
94
+ :attr: `~annotationlib.Format.SOURCE ` format (which returns annotations as strings).
95
+
96
+ This example shows how these formats behave:
97
+
98
+ .. doctest ::
99
+
100
+ >>> from annotationlib import get_annotations, Format
101
+ >>> def func (arg : Undefined):
102
+ ... pass
103
+ >>> get_annotations(func, format = Format.VALUE )
104
+ Traceback (most recent call last):
105
+ ...
106
+ NameError: name 'Undefined' is not defined
107
+ >>> get_annotations(func, format = Format.FORWARDREF )
108
+ {'arg': ForwardRef('Undefined')}
109
+ >>> get_annotations(func, format = Format.SOURCE )
110
+ {'arg': 'Undefined'}
111
+
112
+ Implications for annotated code
113
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114
+
115
+ If you define annotations in your code (for example, for use with a static type
116
+ checker), then this change probably does not affect you: you can keep
117
+ writing annotations the same way you did with previous versions of Python.
118
+
119
+ You will likely be able to remove quoted strings in annotations, which are frequently
120
+ used for forward references. Similarly, if you use ``from __future__ import annotations ``
121
+ to avoid having to write strings in annotations, you may well be able to
122
+ remove that import. However, if you rely on third-party libraries that read annotations,
123
+ those libraries may need changes to support unquoted annotations before they
124
+ work as expected.
125
+
126
+ Implications for readers of ``__annotations__ ``
127
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128
+
129
+ If your code reads the ``__annotations__ `` attribute on objects, you may want
130
+ to make changes in order to support code that relies on deferred evaluation of
131
+ annotations. For example, you may want to use :func: `annotationlib.get_annotations `
132
+ with the :attr: `~annotationlib.Format.FORWARDREF ` format, as the :mod: `dataclasses `
133
+ module now does.
134
+
135
+ Related changes
136
+ ^^^^^^^^^^^^^^^
137
+
138
+ The changes in Python 3.14 are designed to rework how ``__annotations__ ``
139
+ works at runtime while minimizing breakage to code that contains
140
+ annotations in source code and to code that reads ``__annotations__ ``. However,
141
+ if you rely on undocumented details of the annotation behavior or on private
142
+ functions in the standard library, there are many ways in which your code may
143
+ not work in Python 3.14. To safeguard your code against future changes,
144
+ use only the documented functionality of the :mod: `annotationlib ` module.
145
+
146
+ ``from __future__ import annotations ``
147
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
148
+
149
+ In Python 3.7, :pep: `563 ` introduced the ``from __future__ import annotations ``
150
+ directive, which turns all annotations into strings. This directive is now
151
+ considered deprecated and it is expected to be removed in a future version of Python.
152
+ However, this removal will not happen until after Python 3.13, the last version of
153
+ Python without deferred evaluation of annotations, reaches its end of life.
154
+ In Python 3.14, the behavior of code using ``from __future__ import annotations ``
155
+ is unchanged.
156
+
157
+
73
158
Improved Error Messages
74
159
-----------------------
75
160
@@ -109,7 +194,9 @@ Other Language Changes
109
194
New Modules
110
195
===========
111
196
112
- * None yet.
197
+ * :mod: `annotationlib `: For introspecting :term: `annotations <annotation> `.
198
+ See :pep: `749 ` for more details.
199
+ (Contributed by Jelle Zijlstra in :gh: `119180 `.)
113
200
114
201
115
202
Improved Modules
0 commit comments