@@ -52,10 +52,10 @@ Type checker authors are encouraged to support syntax features from
52
52
post-3.7 Python versions, although type stub authors should not use such
53
53
features if they wish to maintain compatibility with all type checkers.
54
54
55
- For example, Python 3.7 added the ``async `` keyword (see PEP 492 [ #pep492 ]_ ).
55
+ For example, Python 3.7 added the ``async `` keyword (see :pep: ` 492 ` ).
56
56
Stub authors should use it to mark coroutines, even if the implementation
57
57
still uses the ``@coroutine `` decorator. On the other hand, type stubs should
58
- not use the positional-only syntax from PEP 570 [ #pep570 ]_ , introduced in
58
+ not use the positional-only syntax from :pep: ` 570 ` , introduced in
59
59
Python 3.8, although type checker authors are encouraged to support it.
60
60
61
61
Stubs are treated as if ``from __future__ import annotations `` is enabled.
@@ -72,7 +72,7 @@ Distribution
72
72
============
73
73
74
74
Type stubs can be distributed with or separately from the implementation;
75
- see PEP 561 [ #pep561 ]_ for more information. The
75
+ see :pep: ` 561 ` for more information. The
76
76
`typeshed <https://github.com/python/typeshed >`_ project
77
77
includes stubs for Python's standard library and several third-party
78
78
packages. The stubs for the standard library are usually distributed with type checkers and do not
@@ -112,7 +112,7 @@ Standard Python comments are accepted everywhere Python syntax allows them.
112
112
Two kinds of structured comments are accepted:
113
113
114
114
* A ``# type: X `` comment at the end of a line that defines a variable,
115
- declaring that the variable has type ``X ``. However, PEP 526-style [ #pep526 ]_
115
+ declaring that the variable has type ``X ``. However, :pep: ` 526 ` -style
116
116
variable annotations are preferred over type comments.
117
117
* A ``# type: ignore `` comment at the end of any line, which suppresses all type
118
118
errors in that line. The type checker mypy supports suppressing certain
@@ -124,7 +124,7 @@ Imports
124
124
125
125
Type stubs distinguish between imports that are re-exported and those
126
126
that are only used internally. Imports are re-exported if they use one of these
127
- forms:[ #pep484 ]_
127
+ forms ( :pep: ` 484 `):
128
128
129
129
* ``import X as X ``
130
130
* ``from Y import X as X ``
@@ -175,7 +175,7 @@ Type checkers support cyclic imports in stub files.
175
175
Built-in Generics
176
176
-----------------
177
177
178
- PEP 585 [ #pep585 ]_ built-in generics are supported and should be used instead
178
+ :pep: ` 585 ` built-in generics are supported and should be used instead
179
179
of the corresponding types from ``typing ``::
180
180
181
181
from collections import defaultdict
@@ -268,17 +268,17 @@ Functions and Methods
268
268
269
269
Function and method definition syntax follows general Python syntax.
270
270
Unless an argument name is prefixed with two underscores (but not suffixed
271
- with two underscores), it can be used as a keyword argument [ #pep484 ]_ ::
271
+ with two underscores), it can be used as a keyword argument ( :pep: ` 484 `) ::
272
272
273
273
# x is positional-only
274
274
# y can be used positionally or as keyword argument
275
275
# z is keyword-only
276
276
def foo(__x, y, *, z): ...
277
277
278
- PEP 570 [ #pep570 ]_ style positional-only parameters are currently not
278
+ :pep: ` 570 ` style positional-only parameters are currently not
279
279
supported.
280
280
281
- If an argument or return type is unannotated, per PEP 484 [ #pep484 ]_ its
281
+ If an argument or return type is unannotated, per :pep: ` 484 ` its
282
282
type is assumed to be ``Any ``. It is preferred to leave unknown
283
283
types unannotated rather than explicitly marking them as ``Any ``, as some
284
284
type checkers can optionally warn about unannotated arguments.
@@ -314,7 +314,7 @@ But::
314
314
@classmethod
315
315
def create_it(cls: _T) -> _T: ... # cls has type _T
316
316
317
- PEP 612 [ #pep612 ]_ parameter specification variables (``ParamSpec ``)
317
+ :pep: ` 612 ` parameter specification variables (``ParamSpec ``)
318
318
are supported in argument and return types::
319
319
320
320
_P = ParamSpec("_P")
@@ -325,7 +325,7 @@ are supported in argument and return types::
325
325
However, ``Concatenate `` from PEP 612 is not yet supported; nor is using
326
326
a ``ParamSpec `` to parameterize a generic class.
327
327
328
- PEP 647 [ #pep647 ]_ type guards are supported.
328
+ :pep: ` 647 ` type guards are supported.
329
329
330
330
Using a function or method body other than the ellipsis literal is currently
331
331
unspecified. Stub authors may experiment with other bodies, but it is up to
@@ -355,7 +355,7 @@ Aliases and NewType
355
355
-------------------
356
356
357
357
Type checkers should accept module-level type aliases, optionally using
358
- ``TypeAlias `` (PEP 613 [ #pep613 ]_ ), e.g.::
358
+ ``TypeAlias `` (:pep: ` 613 ` ), e.g.::
359
359
360
360
_IntList = list[int]
361
361
_StrList: TypeAlias = list[str]
@@ -370,7 +370,7 @@ e.g.::
370
370
def f(self) -> int: ...
371
371
g = f
372
372
373
- A type alias may contain type variables. As per PEP 484 [ #pep484 ]_ ,
373
+ A type alias may contain type variables. As per :pep: ` 484 ` ,
374
374
all type variables must be substituted when the alias is used::
375
375
376
376
_K = TypeVar("_K")
@@ -521,7 +521,7 @@ Unsupported Features
521
521
Currently, the following features are not supported by all type checkers
522
522
and should not be used in stubs:
523
523
524
- * Positional-only argument syntax (PEP 570 [ #pep570 ]_ ). Instead, use
524
+ * Positional-only argument syntax (:pep: ` 570 ` ). Instead, use
525
525
the syntax described in the section :ref: `supported-functions `.
526
526
[#ts-4972 ]_
527
527
@@ -607,7 +607,7 @@ Structural Types
607
607
608
608
As seen in the example with ``_Readable `` in the previous section, a common use
609
609
of stub-only objects is to model types that are best described by their
610
- structure. These objects are called protocols [ #pep544 ]_ , and it is encouraged
610
+ structure. These objects are called protocols ( :pep: ` 544 `) , and it is encouraged
611
611
to use them freely to describe simple structural types.
612
612
613
613
It is `recommended <#private-definitions >`_ to prefix stubs-only object names with ``_ ``.
@@ -765,8 +765,8 @@ who wish to provide a consistent style for type stubs. Type checkers
765
765
should not reject stubs that do not follow these recommendations, but
766
766
linters can warn about them.
767
767
768
- Stub files should generally follow the Style Guide for Python Code (PEP 8 )
769
- [ #pep8 ]_ and the :ref: `best-practices `. There are a few exceptions, outlined below, that take the
768
+ Stub files should generally follow the Style Guide for Python Code (:pep: ` 8 ` )
769
+ and the :ref: `best-practices `. There are a few exceptions, outlined below, that take the
770
770
different structure of stub files into account and are aimed to create
771
771
more concise files.
772
772
@@ -989,23 +989,6 @@ No::
989
989
References
990
990
==========
991
991
992
- PEPs
993
- ----
994
-
995
- .. [#pep8 ] PEP 8 -- Style Guide for Python Code, van Rossum et al. (https://www.python.org/dev/peps/pep-0008/)
996
- .. [#pep484 ] PEP 484 -- Type Hints, van Rossum et al. (https://www.python.org/dev/peps/pep-0484)
997
- .. [#pep492 ] PEP 492 -- Coroutines with async and await syntax, Selivanov (https://www.python.org/dev/peps/pep-0492/)
998
- .. [#pep526 ] PEP 526 -- Syntax for Variable Annotations, Gonzalez et al. (https://www.python.org/dev/peps/pep-0526)
999
- .. [#pep544 ] PEP 544 -- Protocols: Structural Subtyping, Levkivskyi et al. (https://www.python.org/dev/peps/pep-0544)
1000
- .. [#pep561 ] PEP 561 -- Distributing and Packaging Type Information, Smith (https://www.python.org/dev/peps/pep-0561)
1001
- .. [#pep570 ] PEP 570 -- Python Positional-Only Parameters, Hastings et al. (https://www.python.org/dev/peps/pep-0570)
1002
- .. [#pep585 ] PEP 585 -- Type Hinting Generics In Standard Collections, Langa (https://www.python.org/dev/peps/pep-0585)
1003
- .. [#pep604 ] PEP 604 -- Allow writing union types as X | Y, Prados and Moss (https://www.python.org/dev/peps/pep-0604)
1004
- .. [#pep612 ] PEP 612 -- Parameter Specification Variables, Mendoza (https://www.python.org/dev/peps/pep-0612)
1005
- .. [#pep613 ] PEP 613 -- Explicit Type Aliases, Zhu (https://www.python.org/dev/peps/pep-0613)
1006
- .. [#pep647 ] PEP 647 -- User-Defined Type Guards, Traut (https://www.python.org/dev/peps/pep-0647)
1007
- .. [#pep3107 ] PEP 3107 -- Function Annotations, Winter and Lownds (https://www.python.org/dev/peps/pep-3107)
1008
-
1009
992
Bugs
1010
993
----
1011
994
0 commit comments