@@ -125,7 +125,7 @@ Note that ``None`` as a type hint is a special case and is replaced by
125
125
NewType
126
126
=======
127
127
128
- Use the :class: `NewType ` helper class to create distinct types::
128
+ Use the :class: `NewType ` helper to create distinct types::
129
129
130
130
from typing import NewType
131
131
@@ -154,7 +154,7 @@ accidentally creating a ``UserId`` in an invalid way::
154
154
155
155
Note that these checks are enforced only by the static type checker. At runtime,
156
156
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
158
158
the expression ``Derived(some_value) `` does not create a new class or introduce
159
159
much overhead beyond that of a regular function call.
160
160
@@ -242,7 +242,7 @@ respectively.
242
242
See :pep: `612 ` for more information.
243
243
244
244
.. seealso ::
245
- The documentation for :class: `ParamSpec ` and :class: `Concatenate ` provide
245
+ The documentation for :class: `ParamSpec ` and :class: `Concatenate ` provides
246
246
examples of usage in ``Callable ``.
247
247
248
248
.. _generics :
@@ -411,7 +411,7 @@ to this is that a list of types can be used to substitute a :class:`ParamSpec`::
411
411
Furthermore, a generic with only one parameter specification variable will accept
412
412
parameter lists in the forms ``X[[Type1, Type2, ...]] `` and also
413
413
``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::
415
415
416
416
>>> class X(Generic[P]): ...
417
417
...
@@ -515,7 +515,7 @@ manner. Use :data:`Any` to indicate that a value is dynamically typed.
515
515
Nominal vs structural subtyping
516
516
===============================
517
517
518
- Initially :pep: `484 ` defined Python static type system as using
518
+ Initially :pep: `484 ` defined the Python static type system as using
519
519
*nominal subtyping *. This means that a class ``A `` is allowed where
520
520
a class ``B `` is expected if and only if ``A `` is a subclass of ``B ``.
521
521
@@ -590,10 +590,10 @@ These can be used as types in annotations and do not support ``[]``.
590
590
* Every type is compatible with :data: `Any `.
591
591
* :data: `Any ` is compatible with every type.
592
592
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.
597
597
598
598
.. data :: LiteralString
599
599
@@ -708,9 +708,9 @@ These can be used as types in annotations and do not support ``[]``.
708
708
709
709
Other common use cases include:
710
710
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.
714
714
715
715
For more information, see :pep: `673 `.
716
716
@@ -880,7 +880,6 @@ These can be used as types in annotations using ``[]``, each having a unique syn
880
880
881
881
def with_lock(f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]:
882
882
'''A type-safe decorator which provides a lock.'''
883
- global my_lock
884
883
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
885
884
# Provide the lock as the first argument.
886
885
return f(my_lock, *args, **kwargs)
@@ -1036,7 +1035,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
1036
1035
``no_type_check `` functionality that currently exists in the ``typing ``
1037
1036
module which completely disables typechecking annotations on a function
1038
1037
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 ``)
1040
1039
together with runtime access to ``x `` within a specific application.
1041
1040
1042
1041
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
1140
1139
2. If the return value is ``True ``, the type of its argument
1141
1140
is the type inside ``TypeGuard ``.
1142
1141
1143
- For example::
1142
+ For example::
1144
1143
1145
1144
def is_str_list(val: list[object]) -> TypeGuard[list[str]]:
1146
1145
'''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
1279
1278
1280
1279
.. class :: TypeVarTuple
1281
1280
1282
- Type variable tuple. A specialized form of :class: `Type variable <TypeVar> `
1281
+ Type variable tuple. A specialized form of :class: `type variable <TypeVar> `
1283
1282
that enables *variadic * generics.
1284
1283
1285
1284
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
1440
1439
use a :class: `TypeVar ` with bound ``Callable[..., Any] ``. However this
1441
1440
causes two problems:
1442
1441
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 ``.
1448
1447
1449
1448
.. attribute :: args
1450
1449
.. attribute :: kwargs
@@ -1602,7 +1601,7 @@ These are not used in annotations. They are building blocks for declaring types.
1602
1601
The resulting class has an extra attribute ``__annotations__ `` giving a
1603
1602
dict that maps the field names to the field types. (The field names are in
1604
1603
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`
1606
1605
API.)
1607
1606
1608
1607
``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.
1695
1694
in 3.13. It may also be unsupported by static type checkers.
1696
1695
1697
1696
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.
1699
1698
Example::
1700
1699
1701
1700
# raises SyntaxError
@@ -1737,7 +1736,7 @@ These are not used in annotations. They are building blocks for declaring types.
1737
1736
y: int
1738
1737
z: int
1739
1738
1740
- A ``TypedDict `` cannot inherit from a non-TypedDict class,
1739
+ A ``TypedDict `` cannot inherit from a non-\ `` TypedDict `` class,
1741
1740
except for :class: `Generic `. For example::
1742
1741
1743
1742
class X(TypedDict):
@@ -2155,7 +2154,7 @@ Corresponding to other types in :mod:`collections.abc`
2155
2154
2156
2155
.. class :: Hashable
2157
2156
2158
- An alias to :class: `collections.abc.Hashable `
2157
+ An alias to :class: `collections.abc.Hashable `.
2159
2158
2160
2159
.. class :: Reversible(Iterable[T_co])
2161
2160
@@ -2167,7 +2166,7 @@ Corresponding to other types in :mod:`collections.abc`
2167
2166
2168
2167
.. class :: Sized
2169
2168
2170
- An alias to :class: `collections.abc.Sized `
2169
+ An alias to :class: `collections.abc.Sized `.
2171
2170
2172
2171
Asynchronous programming
2173
2172
""""""""""""""""""""""""
@@ -2388,12 +2387,12 @@ Functions and decorators
2388
2387
2389
2388
.. seealso ::
2390
2389
`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
2392
2391
information about exhaustiveness checking with static typing.
2393
2392
2394
2393
.. versionadded :: 3.11
2395
2394
2396
- .. function :: reveal_type(obj)
2395
+ .. function :: reveal_type(obj, / )
2397
2396
2398
2397
Reveal the inferred static type of an expression.
2399
2398
@@ -2465,9 +2464,9 @@ Functions and decorators
2465
2464
the documentation for :func: `@overload <overload> `,
2466
2465
``get_overloads(process) `` will return a sequence of three function objects
2467
2466
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.
2469
2468
2470
- ``get_overloads `` can be used for introspecting an overloaded function at
2469
+ ``get_overloads() `` can be used for introspecting an overloaded function at
2471
2470
runtime.
2472
2471
2473
2472
.. versionadded :: 3.11
@@ -2493,7 +2492,7 @@ Functions and decorators
2493
2492
...
2494
2493
class Sub(Base):
2495
2494
def done(self) -> None: # Error reported by type checker
2496
- ...
2495
+ ...
2497
2496
2498
2497
@final
2499
2498
class Leaf:
@@ -2632,8 +2631,8 @@ Introspection helpers
2632
2631
.. class :: ForwardRef
2633
2632
2634
2633
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
2637
2636
a user, but may be used by introspection tools.
2638
2637
2639
2638
.. note ::
@@ -2667,7 +2666,7 @@ Constant
2667
2666
If ``from __future__ import annotations `` is used in Python 3.7 or later,
2668
2667
annotations are not evaluated at function definition time.
2669
2668
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
2671
2670
(see :pep: `563 `).
2672
2671
2673
2672
.. versionadded :: 3.5.2
0 commit comments