Skip to content

Commit 31d9a88

Browse files
[3.10] Improve the typing docs (GH-92264) (#92270)
Co-authored-by: Alex Waygood <[email protected]>. (cherry picked from commit 27e3665) Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 666820c commit 31d9a88

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

Doc/library/typing.rst

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Note that ``None`` as a type hint is a special case and is replaced by
115115
NewType
116116
=======
117117

118-
Use the :class:`NewType` helper class to create distinct types::
118+
Use the :class:`NewType` helper to create distinct types::
119119

120120
from typing import NewType
121121

@@ -144,7 +144,7 @@ accidentally creating a ``UserId`` in an invalid way::
144144

145145
Note that these checks are enforced only by the static type checker. At runtime,
146146
the statement ``Derived = NewType('Derived', Base)`` will make ``Derived`` a
147-
class that immediately returns whatever parameter you pass it. That means
147+
callable that immediately returns whatever parameter you pass it. That means
148148
the expression ``Derived(some_value)`` does not create a new class or introduce
149149
much overhead beyond that of a regular function call.
150150

@@ -232,7 +232,7 @@ respectively.
232232
See :pep:`612` for more information.
233233

234234
.. seealso::
235-
The documentation for :class:`ParamSpec` and :class:`Concatenate` provide
235+
The documentation for :class:`ParamSpec` and :class:`Concatenate` provides
236236
examples of usage in ``Callable``.
237237

238238
.. _generics:
@@ -401,7 +401,7 @@ to this is that a list of types can be used to substitute a :class:`ParamSpec`::
401401
Furthermore, a generic with only one parameter specification variable will accept
402402
parameter lists in the forms ``X[[Type1, Type2, ...]]`` and also
403403
``X[Type1, Type2, ...]`` for aesthetic reasons. Internally, the latter is converted
404-
to the former and are thus equivalent::
404+
to the former, so the following are equivalent::
405405

406406
>>> class X(Generic[P]): ...
407407
...
@@ -505,7 +505,7 @@ manner. Use :data:`Any` to indicate that a value is dynamically typed.
505505
Nominal vs structural subtyping
506506
===============================
507507

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

@@ -756,7 +756,6 @@ These can be used as types in annotations using ``[]``, each having a unique syn
756756

757757
def with_lock(f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]:
758758
'''A type-safe decorator which provides a lock.'''
759-
global my_lock
760759
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
761760
# Provide the lock as the first argument.
762761
return f(my_lock, *args, **kwargs)
@@ -912,7 +911,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
912911
``no_type_check`` functionality that currently exists in the ``typing``
913912
module which completely disables typechecking annotations on a function
914913
or a class, the ``Annotated`` type allows for both static typechecking
915-
of ``T`` (e.g., via mypy or Pyre, which can safely ignore ``x``)
914+
of ``T`` (which can safely ignore ``x``)
916915
together with runtime access to ``x`` within a specific application.
917916

918917
Ultimately, the responsibility of how to interpret the annotations (if
@@ -1016,7 +1015,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
10161015
2. If the return value is ``True``, the type of its argument
10171016
is the type inside ``TypeGuard``.
10181017

1019-
For example::
1018+
For example::
10201019

10211020
def is_str_list(val: List[object]) -> TypeGuard[List[str]]:
10221021
'''Determines whether all objects in the list are strings'''
@@ -1230,11 +1229,11 @@ These are not used in annotations. They are building blocks for creating generic
12301229
use a :class:`TypeVar` with bound ``Callable[..., Any]``. However this
12311230
causes two problems:
12321231

1233-
1. The type checker can't type check the ``inner`` function because
1234-
``*args`` and ``**kwargs`` have to be typed :data:`Any`.
1235-
2. :func:`~cast` may be required in the body of the ``add_logging``
1236-
decorator when returning the ``inner`` function, or the static type
1237-
checker must be told to ignore the ``return inner``.
1232+
1. The type checker can't type check the ``inner`` function because
1233+
``*args`` and ``**kwargs`` have to be typed :data:`Any`.
1234+
2. :func:`~cast` may be required in the body of the ``add_logging``
1235+
decorator when returning the ``inner`` function, or the static type
1236+
checker must be told to ignore the ``return inner``.
12381237

12391238
.. attribute:: args
12401239
.. attribute:: kwargs
@@ -1392,7 +1391,7 @@ These are not used in annotations. They are building blocks for declaring types.
13921391
The resulting class has an extra attribute ``__annotations__`` giving a
13931392
dict that maps the field names to the field types. (The field names are in
13941393
the ``_fields`` attribute and the default values are in the
1395-
``_field_defaults`` attribute both of which are part of the namedtuple
1394+
``_field_defaults`` attribute, both of which are part of the :func:`~collections.namedtuple`
13961395
API.)
13971396

13981397
``NamedTuple`` subclasses can also have docstrings and methods::
@@ -1467,7 +1466,7 @@ These are not used in annotations. They are building blocks for declaring types.
14671466
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
14681467

14691468
The functional syntax should also be used when any of the keys are not valid
1470-
:ref:`identifiers`, for example because they are keywords or contain hyphens.
1469+
:ref:`identifiers <identifiers>`, for example because they are keywords or contain hyphens.
14711470
Example::
14721471

14731472
# raises SyntaxError
@@ -1506,7 +1505,7 @@ These are not used in annotations. They are building blocks for declaring types.
15061505
y: int
15071506
z: int
15081507

1509-
A ``TypedDict`` cannot inherit from a non-TypedDict class,
1508+
A ``TypedDict`` cannot inherit from a non-\ ``TypedDict`` class,
15101509
notably including :class:`Generic`. For example::
15111510

15121511
class X(TypedDict):
@@ -1915,7 +1914,7 @@ Corresponding to other types in :mod:`collections.abc`
19151914

19161915
.. class:: Hashable
19171916

1918-
An alias to :class:`collections.abc.Hashable`
1917+
An alias to :class:`collections.abc.Hashable`.
19191918

19201919
.. class:: Reversible(Iterable[T_co])
19211920

@@ -1927,7 +1926,7 @@ Corresponding to other types in :mod:`collections.abc`
19271926

19281927
.. class:: Sized
19291928

1930-
An alias to :class:`collections.abc.Sized`
1929+
An alias to :class:`collections.abc.Sized`.
19311930

19321931
Asynchronous programming
19331932
""""""""""""""""""""""""
@@ -2132,7 +2131,7 @@ Functions and decorators
21322131
...
21332132
class Sub(Base):
21342133
def done(self) -> None: # Error reported by type checker
2135-
...
2134+
...
21362135

21372136
@final
21382137
class Leaf:
@@ -2293,8 +2292,8 @@ Constant
22932292

22942293
If ``from __future__ import annotations`` is used in Python 3.7 or later,
22952294
annotations are not evaluated at function definition time.
2296-
Instead, they are stored as strings in ``__annotations__``,
2297-
This makes it unnecessary to use quotes around the annotation.
2295+
Instead, they are stored as strings in ``__annotations__``.
2296+
This makes it unnecessary to use quotes around the annotation
22982297
(see :pep:`563`).
22992298

23002299
.. versionadded:: 3.5.2

0 commit comments

Comments
 (0)