Skip to content

Commit 954dd2f

Browse files
[3.14] gh-149995: Update typing.py docstrings and documentation (#150216)
[3.14] 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)
1 parent fe93e90 commit 954dd2f

3 files changed

Lines changed: 83 additions & 82 deletions

File tree

Doc/library/typing.rst

Lines changed: 25 additions & 25 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
@@ -1292,10 +1292,10 @@ These can be used as types in annotations. They all support subscription using
12921292

12931293
:data:`ClassVar` accepts only types and cannot be further subscribed.
12941294

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

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

13661366
def mutate_movie(m: Movie) -> None:
13671367
m["year"] = 1999 # allowed
1368-
m["title"] = "The Matrix" # typechecker error
1368+
m["title"] = "The Matrix" # type checker error
13691369

13701370
There is no runtime checking for this property.
13711371

@@ -2381,9 +2381,9 @@ types.
23812381

23822382
Fields with a default value must come after any fields without a default.
23832383

2384-
The resulting class has an extra attribute ``__annotations__`` giving a
2385-
dict that maps the field names to the field types. (The field names are in
2386-
the ``_fields`` attribute and the default values are in the
2384+
The types for each field name can be retrieved by calling
2385+
:func:`annotationlib.get_annotations` on the resulting class. (The field
2386+
names are in the ``_fields`` attribute and the default values are in the
23872387
``_field_defaults`` attribute, both of which are part of the :func:`~collections.namedtuple`
23882388
API.)
23892389

@@ -2457,7 +2457,7 @@ types.
24572457

24582458
Helper class to create low-overhead :ref:`distinct types <distinct>`.
24592459

2460-
A ``NewType`` is considered a distinct type by a typechecker. At runtime,
2460+
A ``NewType`` is considered a distinct type by a type checker. At runtime,
24612461
however, calling a ``NewType`` returns its argument unchanged.
24622462

24632463
Usage::
@@ -2532,7 +2532,7 @@ types.
25322532
Mark a protocol class as a runtime protocol.
25332533

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

25382538
@runtime_checkable
@@ -2723,9 +2723,9 @@ types.
27232723
key: T
27242724
group: list[T]
27252725

2726-
A ``TypedDict`` can be introspected via annotations dicts
2727-
(see :ref:`annotations-howto` for more information on annotations best practices),
2728-
:attr:`__total__`, :attr:`__required_keys__`, and :attr:`__optional_keys__`.
2726+
A ``TypedDict`` can be introspected via :func:`annotationlib.get_annotations`
2727+
(see :ref:`annotations-howto` for more information on annotations best practices)
2728+
and the following attributes:
27292729

27302730
.. attribute:: __total__
27312731

@@ -2766,7 +2766,7 @@ types.
27662766

27672767
For backwards compatibility with Python 3.10 and below,
27682768
it is also possible to use inheritance to declare both required and
2769-
non-required keys in the same ``TypedDict`` . This is done by declaring a
2769+
non-required keys in the same ``TypedDict``. This is done by declaring a
27702770
``TypedDict`` with one value for the ``total`` argument and then
27712771
inheriting from it in another ``TypedDict`` with a different value for
27722772
``total``:
@@ -2848,34 +2848,34 @@ with :deco:`runtime_checkable`.
28482848

28492849
.. class:: SupportsAbs
28502850

2851-
An ABC with one abstract method ``__abs__`` that is covariant
2851+
A protocol with one abstract method ``__abs__`` that is covariant
28522852
in its return type.
28532853

28542854
.. class:: SupportsBytes
28552855

2856-
An ABC with one abstract method ``__bytes__``.
2856+
A protocol with one abstract method ``__bytes__``.
28572857

28582858
.. class:: SupportsComplex
28592859

2860-
An ABC with one abstract method ``__complex__``.
2860+
A protocol with one abstract method ``__complex__``.
28612861

28622862
.. class:: SupportsFloat
28632863

2864-
An ABC with one abstract method ``__float__``.
2864+
A protocol with one abstract method ``__float__``.
28652865

28662866
.. class:: SupportsIndex
28672867

2868-
An ABC with one abstract method ``__index__``.
2868+
A protocol with one abstract method ``__index__``.
28692869

28702870
.. versionadded:: 3.8
28712871

28722872
.. class:: SupportsInt
28732873

2874-
An ABC with one abstract method ``__int__``.
2874+
A protocol with one abstract method ``__int__``.
28752875

28762876
.. class:: SupportsRound
28772877

2878-
An ABC with one abstract method ``__round__``
2878+
A protocol with one abstract method ``__round__``
28792879
that is covariant in its return type.
28802880

28812881
.. _typing-io:
@@ -3595,7 +3595,7 @@ Constant
35953595

35963596
.. data:: TYPE_CHECKING
35973597

3598-
A special constant that is assumed to be ``True`` by 3rd party static
3598+
A special constant that is assumed to be ``True`` by static
35993599
type checkers. It's ``False`` at runtime.
36003600

36013601
A module which is expensive to import, and which only contain types

0 commit comments

Comments
 (0)