Skip to content

Commit 9a89f1b

Browse files
authored
gh-105286: Further improvements to typing.py docstrings (#105363)
1 parent 5f65ff0 commit 9a89f1b

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

Lib/typing.py

+43-25
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.
@@ -16,6 +13,9 @@
1613
SupportsFloat, SupportsIndex, SupportsAbs, and others.
1714
* Special types: NewType, NamedTuple, TypedDict.
1815
* Deprecated aliases for builtin types and collections.abc ABCs.
16+
17+
Any name not present in __all__ is an implementation detail
18+
that may be changed without notice. Use at your own risk!
1919
"""
2020

2121
from abc import abstractmethod, ABCMeta
@@ -208,10 +208,12 @@ def _should_unflatten_callable_args(typ, args):
208208
"""Internal helper for munging collections.abc.Callable's __args__.
209209
210210
The canonical representation for a Callable's __args__ flattens the
211-
argument types, see https://bugs.python.org/issue42195. For example::
211+
argument types, see https://github.com/python/cpython/issues/86361.
212212
213-
collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
214-
collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
213+
For example::
214+
215+
assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
216+
assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
215217
216218
As a result, if we need to reconstruct the Callable from its __args__,
217219
we need to unflatten it.
@@ -345,8 +347,9 @@ def _flatten_literal_params(parameters):
345347

346348

347349
def _tp_cache(func=None, /, *, typed=False):
348-
"""Internal wrapper caching __getitem__ of generic types with a fallback to
349-
original function for non-hashable arguments.
350+
"""Internal wrapper caching __getitem__ of generic types.
351+
352+
For non-hashable arguments, the original function is used as a fallback.
350353
"""
351354
def decorator(func):
352355
# The callback 'inner' references the newly created lru_cache
@@ -627,10 +630,12 @@ def ClassVar(self, parameters):
627630
628631
An annotation wrapped in ClassVar indicates that a given
629632
attribute is intended to be used as a class variable and
630-
should not be set on instances of that class. Usage::
633+
should not be set on instances of that class.
634+
635+
Usage::
631636
632637
class Starship:
633-
stats: ClassVar[Dict[str, int]] = {} # class variable
638+
stats: ClassVar[dict[str, int]] = {} # class variable
634639
damage: int = 10 # instance variable
635640
636641
ClassVar accepts only types and cannot be further subscribed.
@@ -763,7 +768,9 @@ def TypeAlias(self, parameters):
763768
764769
Use TypeAlias to indicate that an assignment should
765770
be recognized as a proper type alias definition by type
766-
checkers. For example::
771+
checkers.
772+
773+
For example::
767774
768775
Predicate: TypeAlias = Callable[..., bool]
769776
@@ -776,8 +783,8 @@ def TypeAlias(self, parameters):
776783
def Concatenate(self, parameters):
777784
"""Special form for annotating higher-order functions.
778785
779-
``Concatenate`` can be sed in conjunction with ``ParamSpec`` and
780-
``Callable`` to represent a higher order function which adds, removes or
786+
``Concatenate`` can be used in conjunction with ``ParamSpec`` and
787+
``Callable`` to represent a higher-order function which adds, removes or
781788
transforms the parameters of a callable.
782789
783790
For example::
@@ -1593,8 +1600,9 @@ def Unpack(self, parameters):
15931600
"""Type unpack operator.
15941601
15951602
The type unpack operator takes the child types from some container type,
1596-
such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'. For
1597-
example::
1603+
such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'.
1604+
1605+
For example::
15981606
15991607
# For some generic class `Foo`:
16001608
Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
@@ -1619,7 +1627,7 @@ class Bar(Generic[*Ts]): ...
16191627
class Bar[*Ts]: ...
16201628
16211629
The operator can also be used along with a `TypedDict` to annotate
1622-
`**kwargs` in a function signature. For instance::
1630+
`**kwargs` in a function signature::
16231631
16241632
class Movie(TypedDict):
16251633
name: str
@@ -1632,7 +1640,7 @@ def foo(**kwargs: Unpack[Movie]): ...
16321640
Note that there is only some runtime checking of this operator. Not
16331641
everything the runtime allows may be accepted by static type checkers.
16341642
1635-
For more information, see PEP 646.
1643+
For more information, see PEPs 646 and 692.
16361644
"""
16371645
item = _type_check(parameters, f'{self} accepts only single type.')
16381646
return _UnpackGenericAlias(origin=self, args=(item,))
@@ -1880,7 +1888,9 @@ def meth(self) -> int:
18801888
...
18811889
18821890
Such classes are primarily used with static type checkers that recognize
1883-
structural subtyping (static duck-typing), for example::
1891+
structural subtyping (static duck-typing).
1892+
1893+
For example::
18841894
18851895
class C:
18861896
def meth(self) -> int:
@@ -2037,7 +2047,7 @@ class Annotated:
20372047
20382048
Annotated[*Ts, Ann1] # NOT valid
20392049
2040-
This would be equivalent to
2050+
This would be equivalent to::
20412051
20422052
Annotated[T1, T2, T3, ..., Ann1]
20432053
@@ -2255,8 +2265,10 @@ def _strip_annotations(t):
22552265
def get_origin(tp):
22562266
"""Get the unsubscripted version of a type.
22572267
2258-
This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
2259-
Annotated, and others. Return None for unsupported types. Examples::
2268+
This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar,
2269+
Annotated, and others. Return None for unsupported types.
2270+
2271+
Examples::
22602272
22612273
assert get_origin(Literal[42]) is Literal
22622274
assert get_origin(int) is None
@@ -2415,7 +2427,9 @@ def overload(func):
24152427
"""Decorator for overloaded functions/methods.
24162428
24172429
In a stub file, place two or more stub definitions for the same
2418-
function in a row, each decorated with @overload. For example::
2430+
function in a row, each decorated with @overload.
2431+
2432+
For example::
24192433
24202434
@overload
24212435
def utf8(value: None) -> None: ...
@@ -2426,7 +2440,7 @@ def utf8(value: str) -> bytes: ...
24262440
24272441
In a non-stub file (i.e. a regular .py file), do the same but
24282442
follow it with an implementation. The implementation should *not*
2429-
be decorated with @overload. For example::
2443+
be decorated with @overload::
24302444
24312445
@overload
24322446
def utf8(value: None) -> None: ...
@@ -2925,7 +2939,9 @@ class Point2D(TypedDict):
29252939
def Required(self, parameters):
29262940
"""Special typing construct to mark a TypedDict key as required.
29272941
2928-
This is mainly useful for total=False TypedDicts. For example::
2942+
This is mainly useful for total=False TypedDicts.
2943+
2944+
For example::
29292945
29302946
class Movie(TypedDict, total=False):
29312947
title: Required[str]
@@ -2967,7 +2983,9 @@ class NewType:
29672983
29682984
NewType(name, tp) is considered a subtype of tp
29692985
by static type checkers. At runtime, NewType(name, tp) returns
2970-
a dummy callable that simply returns its argument. Usage::
2986+
a dummy callable that simply returns its argument.
2987+
2988+
Usage::
29712989
29722990
UserId = NewType('UserId', int)
29732991

0 commit comments

Comments
 (0)