Skip to content

Commit 27e3665

Browse files
Improve the typing docs (#92264)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 1f631ae commit 27e3665

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

Doc/library/typing.rst

+33-34
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Note that ``None`` as a type hint is a special case and is replaced by
125125
NewType
126126
=======
127127

128-
Use the :class:`NewType` helper class to create distinct types::
128+
Use the :class:`NewType` helper to create distinct types::
129129

130130
from typing import NewType
131131

@@ -154,7 +154,7 @@ accidentally creating a ``UserId`` in an invalid way::
154154

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

@@ -242,7 +242,7 @@ respectively.
242242
See :pep:`612` for more information.
243243

244244
.. seealso::
245-
The documentation for :class:`ParamSpec` and :class:`Concatenate` provide
245+
The documentation for :class:`ParamSpec` and :class:`Concatenate` provides
246246
examples of usage in ``Callable``.
247247

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

416416
>>> class X(Generic[P]): ...
417417
...
@@ -515,7 +515,7 @@ manner. Use :data:`Any` to indicate that a value is dynamically typed.
515515
Nominal vs structural subtyping
516516
===============================
517517

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

@@ -590,10 +590,10 @@ These can be used as types in annotations and do not support ``[]``.
590590
* Every type is compatible with :data:`Any`.
591591
* :data:`Any` is compatible with every type.
592592

593-
.. versionchanged:: 3.11
594-
:data:`Any` can now be used as a base class. This can be useful for
595-
avoiding type checker errors with classes that can duck type anywhere or
596-
are highly dynamic.
593+
.. versionchanged:: 3.11
594+
:data:`Any` can now be used as a base class. This can be useful for
595+
avoiding type checker errors with classes that can duck type anywhere or
596+
are highly dynamic.
597597

598598
.. data:: LiteralString
599599

@@ -708,9 +708,9 @@ These can be used as types in annotations and do not support ``[]``.
708708

709709
Other common use cases include:
710710

711-
- :class:`classmethod`\s that are used as alternative constructors and return instances
712-
of the ``cls`` parameter.
713-
- Annotating an :meth:`object.__enter__` method which returns self.
711+
- :class:`classmethod`\s that are used as alternative constructors and return instances
712+
of the ``cls`` parameter.
713+
- Annotating an :meth:`~object.__enter__` method which returns self.
714714

715715
For more information, see :pep:`673`.
716716

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

881881
def with_lock(f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]:
882882
'''A type-safe decorator which provides a lock.'''
883-
global my_lock
884883
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
885884
# Provide the lock as the first argument.
886885
return f(my_lock, *args, **kwargs)
@@ -1036,7 +1035,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
10361035
``no_type_check`` functionality that currently exists in the ``typing``
10371036
module which completely disables typechecking annotations on a function
10381037
or a class, the ``Annotated`` type allows for both static typechecking
1039-
of ``T`` (e.g., via mypy or Pyre, which can safely ignore ``x``)
1038+
of ``T`` (which can safely ignore ``x``)
10401039
together with runtime access to ``x`` within a specific application.
10411040

10421041
Ultimately, the responsibility of how to interpret the annotations (if
@@ -1140,7 +1139,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
11401139
2. If the return value is ``True``, the type of its argument
11411140
is the type inside ``TypeGuard``.
11421141

1143-
For example::
1142+
For example::
11441143

11451144
def is_str_list(val: list[object]) -> TypeGuard[list[str]]:
11461145
'''Determines whether all objects in the list are strings'''
@@ -1279,7 +1278,7 @@ These are not used in annotations. They are building blocks for creating generic
12791278

12801279
.. class:: TypeVarTuple
12811280

1282-
Type variable tuple. A specialized form of :class:`Type variable <TypeVar>`
1281+
Type variable tuple. A specialized form of :class:`type variable <TypeVar>`
12831282
that enables *variadic* generics.
12841283

12851284
A normal type variable enables parameterization with a single type. A type
@@ -1440,11 +1439,11 @@ These are not used in annotations. They are building blocks for creating generic
14401439
use a :class:`TypeVar` with bound ``Callable[..., Any]``. However this
14411440
causes two problems:
14421441

1443-
1. The type checker can't type check the ``inner`` function because
1444-
``*args`` and ``**kwargs`` have to be typed :data:`Any`.
1445-
2. :func:`~cast` may be required in the body of the ``add_logging``
1446-
decorator when returning the ``inner`` function, or the static type
1447-
checker must be told to ignore the ``return inner``.
1442+
1. The type checker can't type check the ``inner`` function because
1443+
``*args`` and ``**kwargs`` have to be typed :data:`Any`.
1444+
2. :func:`~cast` may be required in the body of the ``add_logging``
1445+
decorator when returning the ``inner`` function, or the static type
1446+
checker must be told to ignore the ``return inner``.
14481447

14491448
.. attribute:: args
14501449
.. attribute:: kwargs
@@ -1602,7 +1601,7 @@ These are not used in annotations. They are building blocks for declaring types.
16021601
The resulting class has an extra attribute ``__annotations__`` giving a
16031602
dict that maps the field names to the field types. (The field names are in
16041603
the ``_fields`` attribute and the default values are in the
1605-
``_field_defaults`` attribute both of which are part of the namedtuple
1604+
``_field_defaults`` attribute, both of which are part of the :func:`~collections.namedtuple`
16061605
API.)
16071606

16081607
``NamedTuple`` subclasses can also have docstrings and methods::
@@ -1695,7 +1694,7 @@ These are not used in annotations. They are building blocks for declaring types.
16951694
in 3.13. It may also be unsupported by static type checkers.
16961695

16971696
The functional syntax should also be used when any of the keys are not valid
1698-
:ref:`identifiers`, for example because they are keywords or contain hyphens.
1697+
:ref:`identifiers <identifiers>`, for example because they are keywords or contain hyphens.
16991698
Example::
17001699

17011700
# raises SyntaxError
@@ -1737,7 +1736,7 @@ These are not used in annotations. They are building blocks for declaring types.
17371736
y: int
17381737
z: int
17391738

1740-
A ``TypedDict`` cannot inherit from a non-TypedDict class,
1739+
A ``TypedDict`` cannot inherit from a non-\ ``TypedDict`` class,
17411740
except for :class:`Generic`. For example::
17421741

17431742
class X(TypedDict):
@@ -2155,7 +2154,7 @@ Corresponding to other types in :mod:`collections.abc`
21552154

21562155
.. class:: Hashable
21572156

2158-
An alias to :class:`collections.abc.Hashable`
2157+
An alias to :class:`collections.abc.Hashable`.
21592158

21602159
.. class:: Reversible(Iterable[T_co])
21612160

@@ -2167,7 +2166,7 @@ Corresponding to other types in :mod:`collections.abc`
21672166

21682167
.. class:: Sized
21692168

2170-
An alias to :class:`collections.abc.Sized`
2169+
An alias to :class:`collections.abc.Sized`.
21712170

21722171
Asynchronous programming
21732172
""""""""""""""""""""""""
@@ -2388,12 +2387,12 @@ Functions and decorators
23882387

23892388
.. seealso::
23902389
`Unreachable Code and Exhaustiveness Checking
2391-
<https://typing.readthedocs.io/en/latest/source/unreachable.html>_` has more
2390+
<https://typing.readthedocs.io/en/latest/source/unreachable.html>`__ has more
23922391
information about exhaustiveness checking with static typing.
23932392

23942393
.. versionadded:: 3.11
23952394

2396-
.. function:: reveal_type(obj)
2395+
.. function:: reveal_type(obj, /)
23972396

23982397
Reveal the inferred static type of an expression.
23992398

@@ -2465,9 +2464,9 @@ Functions and decorators
24652464
the documentation for :func:`@overload <overload>`,
24662465
``get_overloads(process)`` will return a sequence of three function objects
24672466
for the three defined overloads. If called on a function with no overloads,
2468-
``get_overloads`` returns an empty sequence.
2467+
``get_overloads()`` returns an empty sequence.
24692468

2470-
``get_overloads`` can be used for introspecting an overloaded function at
2469+
``get_overloads()`` can be used for introspecting an overloaded function at
24712470
runtime.
24722471

24732472
.. versionadded:: 3.11
@@ -2493,7 +2492,7 @@ Functions and decorators
24932492
...
24942493
class Sub(Base):
24952494
def done(self) -> None: # Error reported by type checker
2496-
...
2495+
...
24972496

24982497
@final
24992498
class Leaf:
@@ -2632,8 +2631,8 @@ Introspection helpers
26322631
.. class:: ForwardRef
26332632

26342633
A class used for internal typing representation of string forward references.
2635-
For example, ``list["SomeClass"]`` is implicitly transformed into
2636-
``list[ForwardRef("SomeClass")]``. This class should not be instantiated by
2634+
For example, ``List["SomeClass"]`` is implicitly transformed into
2635+
``List[ForwardRef("SomeClass")]``. This class should not be instantiated by
26372636
a user, but may be used by introspection tools.
26382637

26392638
.. note::
@@ -2667,7 +2666,7 @@ Constant
26672666
If ``from __future__ import annotations`` is used in Python 3.7 or later,
26682667
annotations are not evaluated at function definition time.
26692668
Instead, they are stored as strings in ``__annotations__``.
2670-
This makes it unnecessary to use quotes around the annotation.
2669+
This makes it unnecessary to use quotes around the annotation
26712670
(see :pep:`563`).
26722671

26732672
.. versionadded:: 3.5.2

0 commit comments

Comments
 (0)