Skip to content

Commit 3a195f3

Browse files
[3.11] gh-105286: Further improvements to typing.py docstrings (GH-105363) (#105417)
[3.11] gh-105286: Further improvements to `typing.py` docstrings (GH-105363). (cherry picked from commit 9a89f1b) Co-authored-by: Alex Waygood <[email protected]>
1 parent ccc9717 commit 3a195f3

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

Lib/typing.py

+41-23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
"""
22
The typing module: Support for gradual typing as defined by PEP 484 and subsequent PEPs.
33
4-
Any name not present in __all__ is an implementation detail
5-
that may be changed without notice. Use at your own risk!
6-
74
Among other things, the module includes the following:
85
* Generic, Protocol, and internal machinery to support generic aliases.
96
All subscripted types like X[int], Union[int, str] are generic aliases.
@@ -17,6 +14,9 @@
1714
* Special types: NewType, NamedTuple, TypedDict.
1815
* Deprecated wrapper submodules for re and io related types.
1916
* Deprecated aliases for builtin types and collections.abc ABCs.
17+
18+
Any name not present in __all__ is an implementation detail
19+
that may be changed without notice. Use at your own risk!
2020
"""
2121

2222
from abc import abstractmethod, ABCMeta
@@ -207,10 +207,12 @@ def _should_unflatten_callable_args(typ, args):
207207
"""Internal helper for munging collections.abc.Callable's __args__.
208208
209209
The canonical representation for a Callable's __args__ flattens the
210-
argument types, see https://bugs.python.org/issue42195. For example::
210+
argument types, see https://github.com/python/cpython/issues/86361.
211211
212-
collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
213-
collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
212+
For example::
213+
214+
assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
215+
assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
214216
215217
As a result, if we need to reconstruct the Callable from its __args__,
216218
we need to unflatten it.
@@ -339,8 +341,9 @@ def _flatten_literal_params(parameters):
339341

340342

341343
def _tp_cache(func=None, /, *, typed=False):
342-
"""Internal wrapper caching __getitem__ of generic types with a fallback to
343-
original function for non-hashable arguments.
344+
"""Internal wrapper caching __getitem__ of generic types.
345+
346+
For non-hashable arguments, the original function is used as a fallback.
344347
"""
345348
def decorator(func):
346349
cached = functools.lru_cache(typed=typed)(func)
@@ -614,10 +617,12 @@ def ClassVar(self, parameters):
614617
615618
An annotation wrapped in ClassVar indicates that a given
616619
attribute is intended to be used as a class variable and
617-
should not be set on instances of that class. Usage::
620+
should not be set on instances of that class.
621+
622+
Usage::
618623
619624
class Starship:
620-
stats: ClassVar[Dict[str, int]] = {} # class variable
625+
stats: ClassVar[dict[str, int]] = {} # class variable
621626
damage: int = 10 # instance variable
622627
623628
ClassVar accepts only types and cannot be further subscribed.
@@ -741,7 +746,9 @@ def TypeAlias(self, parameters):
741746
742747
Use TypeAlias to indicate that an assignment should
743748
be recognized as a proper type alias definition by type
744-
checkers. For example::
749+
checkers.
750+
751+
For example::
745752
746753
Predicate: TypeAlias = Callable[..., bool]
747754
@@ -754,8 +761,8 @@ def TypeAlias(self, parameters):
754761
def Concatenate(self, parameters):
755762
"""Special form for annotating higher-order functions.
756763
757-
``Concatenate`` can be sed in conjunction with ``ParamSpec`` and
758-
``Callable`` to represent a higher order function which adds, removes or
764+
``Concatenate`` can be used in conjunction with ``ParamSpec`` and
765+
``Callable`` to represent a higher-order function which adds, removes or
759766
transforms the parameters of a callable.
760767
761768
For example::
@@ -1713,8 +1720,9 @@ def Unpack(self, parameters):
17131720
"""Type unpack operator.
17141721
17151722
The type unpack operator takes the child types from some container type,
1716-
such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'. For
1717-
example::
1723+
such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'.
1724+
1725+
For example::
17181726
17191727
# For some generic class `Foo`:
17201728
Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
@@ -2007,7 +2015,9 @@ def meth(self) -> int:
20072015
...
20082016
20092017
Such classes are primarily used with static type checkers that recognize
2010-
structural subtyping (static duck-typing), for example::
2018+
structural subtyping (static duck-typing).
2019+
2020+
For example::
20112021
20122022
class C:
20132023
def meth(self) -> int:
@@ -2184,7 +2194,7 @@ class Annotated:
21842194
21852195
Annotated[*Ts, Ann1] # NOT valid
21862196
2187-
This would be equivalent to
2197+
This would be equivalent to::
21882198
21892199
Annotated[T1, T2, T3, ..., Ann1]
21902200
@@ -2402,8 +2412,10 @@ def _strip_annotations(t):
24022412
def get_origin(tp):
24032413
"""Get the unsubscripted version of a type.
24042414
2405-
This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
2406-
Annotated, and others. Return None for unsupported types. Examples::
2415+
This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar,
2416+
Annotated, and others. Return None for unsupported types.
2417+
2418+
Examples::
24072419
24082420
assert get_origin(Literal[42]) is Literal
24092421
assert get_origin(int) is None
@@ -2562,7 +2574,9 @@ def overload(func):
25622574
"""Decorator for overloaded functions/methods.
25632575
25642576
In a stub file, place two or more stub definitions for the same
2565-
function in a row, each decorated with @overload. For example::
2577+
function in a row, each decorated with @overload.
2578+
2579+
For example::
25662580
25672581
@overload
25682582
def utf8(value: None) -> None: ...
@@ -2573,7 +2587,7 @@ def utf8(value: str) -> bytes: ...
25732587
25742588
In a non-stub file (i.e. a regular .py file), do the same but
25752589
follow it with an implementation. The implementation should *not*
2576-
be decorated with @overload. For example::
2590+
be decorated with @overload::
25772591
25782592
@overload
25792593
def utf8(value: None) -> None: ...
@@ -3075,7 +3089,9 @@ class Point2D(TypedDict):
30753089
def Required(self, parameters):
30763090
"""Special typing construct to mark a TypedDict key as required.
30773091
3078-
This is mainly useful for total=False TypedDicts. For example::
3092+
This is mainly useful for total=False TypedDicts.
3093+
3094+
For example::
30793095
30803096
class Movie(TypedDict, total=False):
30813097
title: Required[str]
@@ -3117,7 +3133,9 @@ class NewType:
31173133
31183134
NewType(name, tp) is considered a subtype of tp
31193135
by static type checkers. At runtime, NewType(name, tp) returns
3120-
a dummy callable that simply returns its argument. Usage::
3136+
a dummy callable that simply returns its argument.
3137+
3138+
Usage::
31213139
31223140
UserId = NewType('UserId', int)
31233141

0 commit comments

Comments
 (0)