@@ -45,21 +45,13 @@ Stub files are syntactically valid Python files in the earliest Python version
45
45
that is not yet end-of-life. They use a ``.pyi `` suffix. The Python syntax used
46
46
by stub files is independent from the Python versions supported by the
47
47
implementation, and from the Python version the type checker runs under (if
48
- any). Therefore, stubs may use the latest syntax features available in
49
- the earliest supported version, even if the implementation supports older
50
- versions.
51
-
52
- For example, Python 3.7 added the ``async `` keyword (see :pep: `492 `). Stubs may
53
- use it to mark coroutines, even if the implementation still uses the
54
- ``@coroutine `` decorator. On the other hand, the ``type `` soft keyword from
55
- :pep: `695 `, introduced in Python 3.12, is not valid in stubs until Python 3.11
56
- reaches end-of-life in October 2027.
57
-
58
- Stubs are treated as if ``from __future__ import annotations `` is enabled. In
59
- particular, forward references do not need to be quoted, and syntax from newer
60
- versions than otherwise supported may be used in annotation contexts. For
61
- example, the pipe union syntax (``X | Y ``) introduced in Python 3.10 may be used
62
- in annotation contexts even before Python 3.9 reaches end-of-life.
48
+ any).
49
+
50
+ Type checkers should treat stubs as if ``from __future__ import annotations `` is
51
+ enabled. In particular, forward references do not need to be quoted, and syntax
52
+ from newer versions than otherwise supported may be used in annotation contexts.
53
+ For example, the pipe union syntax (``X | Y ``) introduced in Python 3.10 may be
54
+ used in annotation contexts even before Python 3.9 reaches end-of-life.
63
55
64
56
.. _stub-file-supported-constructs :
65
57
@@ -73,10 +65,7 @@ Type checkers should fully support these constructs:
73
65
(``type: ignore ``) comments
74
66
* Import statements, including the standard :ref: `import-conventions ` and cyclic
75
67
imports
76
- * Module-level type aliases (e.g., ``X: TypeAlias = int ``,
77
- ``Y: TypeAlias = dict[str, _V] ``)
78
- * Regular aliases (e.g., ``function_alias = some_function ``) at both the module-
79
- and class-level
68
+ * Aliases, including type aliases, at both the module and class level
80
69
* :ref: `Simple version and platform checks <version-and-platform-checks >`
81
70
82
71
The constructs in the following subsections may be supported in a more limited
@@ -92,6 +81,8 @@ support the following expressions:
92
81
* The ellipsis literal, ``... ``, which can stand in for any value
93
82
* Any value that is a
94
83
:ref: `legal parameter for typing.Literal <literal-legal-parameters >`
84
+ * Floating point literals, such as `3.14 `
85
+ * Complex literals, such as `1 + 2j `
95
86
96
87
Module Level Attributes
97
88
"""""""""""""""""""""""
@@ -104,8 +95,8 @@ assignments::
104
95
x = 0 # type: int
105
96
x = ... # type: int
106
97
107
- A variable annotated as `` Final `` and assigned a literal value has the
108
- corresponding `` Literal `` type ::
98
+ The :ref: ` Literal shortcut using Final < literal-final-interactions >` should be
99
+ supported ::
109
100
110
101
x: Final = 0 # type is Literal[0]
111
102
@@ -127,7 +118,7 @@ are expected to understand only the following constructs in class bodies:
127
118
* Instance attributes follow the same rules as module level attributes
128
119
(see above).
129
120
* Method definitions (see below) and properties.
130
- * Method aliases .
121
+ * Aliases .
131
122
* Inner class definitions.
132
123
133
124
Yes::
@@ -140,6 +131,7 @@ Yes::
140
131
def read_only(self) -> int: ...
141
132
def do_stuff(self, y: str) -> None: ...
142
133
doStuff = do_stuff
134
+ IntList: TypeAlias = list[int]
143
135
class Inner: ...
144
136
145
137
Functions and Methods
@@ -164,6 +156,7 @@ in the ``typing`` module, plus these additional ones:
164
156
* ``property `` (including ``.setter `` and ``.deleter ``)
165
157
* ``abc.abstractmethod ``
166
158
* ``dataclasses.dataclass ``
159
+ * ``warnings.deprecated ``
167
160
* functions decorated with ``@typing.dataclass_transform ``
168
161
169
162
The Typeshed Project
@@ -410,9 +403,5 @@ The following import forms re-export symbols:
410
403
* ``import X as X `` (a redundant module alias): re-exports ``X ``.
411
404
* ``from Y import X as X `` (a redundant symbol alias): re-exports ``X ``.
412
405
* ``from Y import * ``: if ``Y `` defines a module-level ``__all__ `` list,
413
- re-exports all names in ``__all__ ``; otherwise, re-exports all symbols in
414
- ``Y `` that do not begin with an underscore.
415
- * ``from . import bar `` in an ``__init__ `` module: re-exports ``bar `` if it does
416
- not begin with an underscore.
417
- * ``from .bar import Bar `` in an ``__init__ `` module: re-exports ``Bar `` if it
418
- does not begin with an underscore.
406
+ re-exports all names in ``__all__ ``; otherwise, re-exports all public symbols
407
+ in ``Y ``'s global scope.
0 commit comments