Skip to content

Commit 187cb95

Browse files
[3.9] Improve the typing docs (GH-92264) (#92271)
Co-authored-by: Alex Waygood <[email protected]>. (cherry picked from commit 27e3665) Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 696d868 commit 187cb95

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Doc/library/typing.rst

+12-12
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Note that ``None`` as a type hint is a special case and is replaced by
103103
NewType
104104
=======
105105

106-
Use the :func:`NewType` helper function to create distinct types::
106+
Use the :func:`NewType` helper to create distinct types::
107107

108108
from typing import NewType
109109

@@ -132,7 +132,7 @@ accidentally creating a ``UserId`` in an invalid way::
132132

133133
Note that these checks are enforced only by the static type checker. At runtime,
134134
the statement ``Derived = NewType('Derived', Base)`` will make ``Derived`` a
135-
function that immediately returns whatever parameter you pass it. That means
135+
callable that immediately returns whatever parameter you pass it. That means
136136
the expression ``Derived(some_value)`` does not create a new class or introduce
137137
any overhead beyond that of a regular function call.
138138

@@ -435,7 +435,7 @@ manner. Use :data:`Any` to indicate that a value is dynamically typed.
435435
Nominal vs structural subtyping
436436
===============================
437437

438-
Initially :pep:`484` defined Python static type system as using
438+
Initially :pep:`484` defined the Python static type system as using
439439
*nominal subtyping*. This means that a class ``A`` is allowed where
440440
a class ``B`` is expected if and only if ``A`` is a subclass of ``B``.
441441

@@ -753,7 +753,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
753753
``no_type_check`` functionality that currently exists in the ``typing``
754754
module which completely disables typechecking annotations on a function
755755
or a class, the ``Annotated`` type allows for both static typechecking
756-
of ``T`` (e.g., via mypy or Pyre, which can safely ignore ``x``)
756+
of ``T`` (which can safely ignore ``x``)
757757
together with runtime access to ``x`` within a specific application.
758758

759759
Ultimately, the responsibility of how to interpret the annotations (if
@@ -1064,7 +1064,7 @@ These are not used in annotations. They are building blocks for declaring types.
10641064
The resulting class has an extra attribute ``__annotations__`` giving a
10651065
dict that maps the field names to the field types. (The field names are in
10661066
the ``_fields`` attribute and the default values are in the
1067-
``_field_defaults`` attribute both of which are part of the namedtuple
1067+
``_field_defaults`` attribute, both of which are part of the :func:`~collections.namedtuple`
10681068
API.)
10691069

10701070
``NamedTuple`` subclasses can also have docstrings and methods::
@@ -1140,7 +1140,7 @@ These are not used in annotations. They are building blocks for declaring types.
11401140
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
11411141

11421142
The functional syntax should also be used when any of the keys are not valid
1143-
:ref:`identifiers`, for example because they are keywords or contain hyphens.
1143+
:ref:`identifiers <identifiers>`, for example because they are keywords or contain hyphens.
11441144
Example::
11451145

11461146
# raises SyntaxError
@@ -1182,7 +1182,7 @@ These are not used in annotations. They are building blocks for declaring types.
11821182
y: int
11831183
z: int
11841184

1185-
A ``TypedDict`` cannot inherit from a non-TypedDict class,
1185+
A ``TypedDict`` cannot inherit from a non-\ ``TypedDict`` class,
11861186
notably including :class:`Generic`. For example::
11871187

11881188
class X(TypedDict):
@@ -1590,7 +1590,7 @@ Corresponding to other types in :mod:`collections.abc`
15901590

15911591
.. class:: Hashable
15921592

1593-
An alias to :class:`collections.abc.Hashable`
1593+
An alias to :class:`collections.abc.Hashable`.
15941594

15951595
.. class:: Reversible(Iterable[T_co])
15961596

@@ -1602,7 +1602,7 @@ Corresponding to other types in :mod:`collections.abc`
16021602

16031603
.. class:: Sized
16041604

1605-
An alias to :class:`collections.abc.Sized`
1605+
An alias to :class:`collections.abc.Sized`.
16061606

16071607
Asynchronous programming
16081608
""""""""""""""""""""""""
@@ -1807,7 +1807,7 @@ Functions and decorators
18071807
...
18081808
class Sub(Base):
18091809
def done(self) -> None: # Error reported by type checker
1810-
...
1810+
...
18111811

18121812
@final
18131813
class Leaf:
@@ -1946,8 +1946,8 @@ Constant
19461946

19471947
If ``from __future__ import annotations`` is used in Python 3.7 or later,
19481948
annotations are not evaluated at function definition time.
1949-
Instead, they are stored as strings in ``__annotations__``,
1950-
This makes it unnecessary to use quotes around the annotation.
1949+
Instead, they are stored as strings in ``__annotations__``.
1950+
This makes it unnecessary to use quotes around the annotation
19511951
(see :pep:`563`).
19521952

19531953
.. versionadded:: 3.5.2

0 commit comments

Comments
 (0)