Skip to content

Commit 01fc70b

Browse files
JelleZijlstramiss-islington
authored andcommitted
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 072246a commit 01fc70b

3 files changed

Lines changed: 81 additions & 81 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
@@ -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

@@ -2472,9 +2472,9 @@ types.
24722472

24732473
Fields with a default value must come after any fields without a default.
24742474

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

@@ -2535,7 +2535,7 @@ types.
25352535

25362536
Helper class to create low-overhead :ref:`distinct types <distinct>`.
25372537

2538-
A ``NewType`` is considered a distinct type by a typechecker. At runtime,
2538+
A ``NewType`` is considered a distinct type by a type checker. At runtime,
25392539
however, calling a ``NewType`` returns its argument unchanged.
25402540

25412541
Usage::
@@ -2616,7 +2616,7 @@ types.
26162616
Mark a protocol class as a runtime protocol.
26172617

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

26222622
@runtime_checkable
@@ -2855,7 +2855,7 @@ types.
28552855
key: T
28562856
group: list[T]
28572857

2858-
A ``TypedDict`` can be introspected via annotations dicts
2858+
A ``TypedDict`` can be introspected via :func:`annotationlib.get_annotations`
28592859
(see :ref:`annotations-howto` for more information on annotations best practices)
28602860
and the following attributes:
28612861

@@ -2898,7 +2898,7 @@ types.
28982898

28992899
For backwards compatibility with Python 3.10 and below,
29002900
it is also possible to use inheritance to declare both required and
2901-
non-required keys in the same ``TypedDict`` . This is done by declaring a
2901+
non-required keys in the same ``TypedDict``. This is done by declaring a
29022902
``TypedDict`` with one value for the ``total`` argument and then
29032903
inheriting from it in another ``TypedDict`` with a different value for
29042904
``total``:
@@ -2982,34 +2982,34 @@ with :deco:`runtime_checkable`.
29822982

29832983
.. class:: SupportsAbs
29842984

2985-
An ABC with one abstract method ``__abs__`` that is covariant
2985+
A protocol with one abstract method ``__abs__`` that is covariant
29862986
in its return type.
29872987

29882988
.. class:: SupportsBytes
29892989

2990-
An ABC with one abstract method ``__bytes__``.
2990+
A protocol with one abstract method ``__bytes__``.
29912991

29922992
.. class:: SupportsComplex
29932993

2994-
An ABC with one abstract method ``__complex__``.
2994+
A protocol with one abstract method ``__complex__``.
29952995

29962996
.. class:: SupportsFloat
29972997

2998-
An ABC with one abstract method ``__float__``.
2998+
A protocol with one abstract method ``__float__``.
29992999

30003000
.. class:: SupportsIndex
30013001

3002-
An ABC with one abstract method ``__index__``.
3002+
A protocol with one abstract method ``__index__``.
30033003

30043004
.. versionadded:: 3.8
30053005

30063006
.. class:: SupportsInt
30073007

3008-
An ABC with one abstract method ``__int__``.
3008+
A protocol with one abstract method ``__int__``.
30093009

30103010
.. class:: SupportsRound
30113011

3012-
An ABC with one abstract method ``__round__``
3012+
A protocol with one abstract method ``__round__``
30133013
that is covariant in its return type.
30143014

30153015
.. _typing-io:
@@ -3763,7 +3763,7 @@ Constant
37633763

37643764
.. data:: TYPE_CHECKING
37653765

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

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

0 commit comments

Comments
 (0)