Skip to content

Commit ef214fb

Browse files
committed
[3.13] gh-149995: Update typing.py docstrings and documentation (GH-149996)
Some of these docstrings read as if they were written when typing.py was first written, and things have evolved since then. A few motivations: - Call protocols protocols instead of ABCs. They are also ABCs, but the fact they are protocols is more relevant to typing. - Avoid recommending direct use of .__annotations__ and steer users to annotationlib instead. - For TypedDict, mention NotRequired before total=False since it is more general and probably more frequently useful. - For overloads, mention runtime use first instead of stub use. I think early on there was talk of allowing overload only in stubs, but it is now heavily used at runtime too and that's more likely to be relevant to users. (cherry picked from commit f159419) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent cfa3daa commit ef214fb

3 files changed

Lines changed: 81 additions & 80 deletions

File tree

Doc/library/typing.rst

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,8 @@ The :data:`Any` type
719719
====================
720720

721721
A special kind of type is :data:`Any`. A static type checker will treat
722-
every type as being compatible with :data:`Any` and :data:`Any` as being
723-
compatible with every type.
722+
every type as assignable to :data:`Any` and :data:`Any` as assignable to
723+
every type.
724724

725725
This means that it is possible to perform any operation or method call on a
726726
value of type :data:`Any` and assign it to any variable::
@@ -785,7 +785,7 @@ it as a return value) of a more specialized type is a type error. For example::
785785
hash_a(42)
786786
hash_a("foo")
787787

788-
# Passes type checking, since Any is compatible with all types
788+
# Passes type checking, since Any is assignable to all types
789789
hash_b(42)
790790
hash_b("foo")
791791

@@ -851,8 +851,8 @@ using ``[]``.
851851

852852
Special type indicating an unconstrained type.
853853

854-
* Every type is compatible with :data:`Any`.
855-
* :data:`Any` is compatible with every type.
854+
* Every type is assignable to :data:`Any`.
855+
* :data:`Any` is assignable to every type.
856856

857857
.. versionchanged:: 3.11
858858
:data:`Any` can now be used as a base class. This can be useful for
@@ -1284,10 +1284,10 @@ These can be used as types in annotations. They all support subscription using
12841284

12851285
:data:`ClassVar` accepts only types and cannot be further subscribed.
12861286

1287-
:data:`ClassVar` is not a class itself, and should not
1287+
:data:`ClassVar` is not a class itself, and cannot
12881288
be used with :func:`isinstance` or :func:`issubclass`.
12891289
:data:`ClassVar` does not change Python runtime behavior, but
1290-
it can be used by third-party type checkers. For example, a type checker
1290+
it can be used by static type checkers. For example, a type checker
12911291
might flag the following code as an error::
12921292

12931293
enterprise_d = Starship(3000)
@@ -1357,7 +1357,7 @@ These can be used as types in annotations. They all support subscription using
13571357

13581358
def mutate_movie(m: Movie) -> None:
13591359
m["year"] = 1999 # allowed
1360-
m["title"] = "The Matrix" # typechecker error
1360+
m["title"] = "The Matrix" # type checker error
13611361

13621362
There is no runtime checking for this property.
13631363

@@ -2283,9 +2283,9 @@ types.
22832283

22842284
Fields with a default value must come after any fields without a default.
22852285

2286-
The resulting class has an extra attribute ``__annotations__`` giving a
2287-
dict that maps the field names to the field types. (The field names are in
2288-
the ``_fields`` attribute and the default values are in the
2286+
The types for each field name can be retrieved by calling
2287+
:func:`inspect.get_annotations` on the resulting class. (The field names
2288+
are in the ``_fields`` attribute and the default values are in the
22892289
``_field_defaults`` attribute, both of which are part of the :func:`~collections.namedtuple`
22902290
API.)
22912291

@@ -2355,7 +2355,7 @@ types.
23552355

23562356
Helper class to create low-overhead :ref:`distinct types <distinct>`.
23572357

2358-
A ``NewType`` is considered a distinct type by a typechecker. At runtime,
2358+
A ``NewType`` is considered a distinct type by a type checker. At runtime,
23592359
however, calling a ``NewType`` returns its argument unchanged.
23602360

23612361
Usage::
@@ -2430,7 +2430,7 @@ types.
24302430
Mark a protocol class as a runtime protocol.
24312431

24322432
Such a protocol can be used with :func:`isinstance` and :func:`issubclass`.
2433-
This allows a simple-minded structural check, very similar to "one trick ponies"
2433+
This allows a simple-minded structural check, very similar to "one-trick ponies"
24342434
in :mod:`collections.abc` such as :class:`~collections.abc.Iterable`. For example::
24352435

24362436
@runtime_checkable
@@ -2621,7 +2621,7 @@ types.
26212621
key: T
26222622
group: list[T]
26232623

2624-
A ``TypedDict`` can be introspected via annotations dicts
2624+
A ``TypedDict`` can be introspected via :func:`inspect.get_annotations`
26252625
(see :ref:`annotations-howto` for more information on annotations best practices),
26262626
:attr:`__total__`, :attr:`__required_keys__`, and :attr:`__optional_keys__`.
26272627

@@ -2664,7 +2664,7 @@ types.
26642664

26652665
For backwards compatibility with Python 3.10 and below,
26662666
it is also possible to use inheritance to declare both required and
2667-
non-required keys in the same ``TypedDict`` . This is done by declaring a
2667+
non-required keys in the same ``TypedDict``. This is done by declaring a
26682668
``TypedDict`` with one value for the ``total`` argument and then
26692669
inheriting from it in another ``TypedDict`` with a different value for
26702670
``total``:
@@ -2746,34 +2746,34 @@ with :deco:`runtime_checkable`.
27462746

27472747
.. class:: SupportsAbs
27482748

2749-
An ABC with one abstract method ``__abs__`` that is covariant
2749+
A protocol with one abstract method ``__abs__`` that is covariant
27502750
in its return type.
27512751

27522752
.. class:: SupportsBytes
27532753

2754-
An ABC with one abstract method ``__bytes__``.
2754+
A protocol with one abstract method ``__bytes__``.
27552755

27562756
.. class:: SupportsComplex
27572757

2758-
An ABC with one abstract method ``__complex__``.
2758+
A protocol with one abstract method ``__complex__``.
27592759

27602760
.. class:: SupportsFloat
27612761

2762-
An ABC with one abstract method ``__float__``.
2762+
A protocol with one abstract method ``__float__``.
27632763

27642764
.. class:: SupportsIndex
27652765

2766-
An ABC with one abstract method ``__index__``.
2766+
A protocol with one abstract method ``__index__``.
27672767

27682768
.. versionadded:: 3.8
27692769

27702770
.. class:: SupportsInt
27712771

2772-
An ABC with one abstract method ``__int__``.
2772+
A protocol with one abstract method ``__int__``.
27732773

27742774
.. class:: SupportsRound
27752775

2776-
An ABC with one abstract method ``__round__``
2776+
A protocol with one abstract method ``__round__``
27772777
that is covariant in its return type.
27782778

27792779
ABCs for working with IO
@@ -3432,7 +3432,7 @@ Constant
34323432

34333433
.. data:: TYPE_CHECKING
34343434

3435-
A special constant that is assumed to be ``True`` by 3rd party static
3435+
A special constant that is assumed to be ``True`` by static
34363436
type checkers. It is ``False`` at runtime.
34373437

34383438
Usage::

0 commit comments

Comments
 (0)