Skip to content

Commit 84133c5

Browse files
[3.12] gh-105286: Further improvements to typing.py docstrings (GH-105363) (#105416)
gh-105286: Further improvements to `typing.py` docstrings (GH-105363) (cherry picked from commit 9a89f1b) Co-authored-by: Alex Waygood <[email protected]>
1 parent 8b89592 commit 84133c5

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

Lib/typing.py

Lines changed: 43 additions & 25 deletions
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
@@ -214,10 +214,12 @@ def _should_unflatten_callable_args(typ, args):
214214
"""Internal helper for munging collections.abc.Callable's __args__.
215215
216216
The canonical representation for a Callable's __args__ flattens the
217-
argument types, see https://bugs.python.org/issue42195. For example::
217+
argument types, see https://github.com/python/cpython/issues/86361.
218218
219-
collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
220-
collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
219+
For example::
220+
221+
assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
222+
assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
221223
222224
As a result, if we need to reconstruct the Callable from its __args__,
223225
we need to unflatten it.
@@ -351,8 +353,9 @@ def _flatten_literal_params(parameters):
351353

352354

353355
def _tp_cache(func=None, /, *, typed=False):
354-
"""Internal wrapper caching __getitem__ of generic types with a fallback to
355-
original function for non-hashable arguments.
356+
"""Internal wrapper caching __getitem__ of generic types.
357+
358+
For non-hashable arguments, the original function is used as a fallback.
356359
"""
357360
def decorator(func):
358361
# The callback 'inner' references the newly created lru_cache
@@ -633,10 +636,12 @@ def ClassVar(self, parameters):
633636
634637
An annotation wrapped in ClassVar indicates that a given
635638
attribute is intended to be used as a class variable and
636-
should not be set on instances of that class. Usage::
639+
should not be set on instances of that class.
640+
641+
Usage::
637642
638643
class Starship:
639-
stats: ClassVar[Dict[str, int]] = {} # class variable
644+
stats: ClassVar[dict[str, int]] = {} # class variable
640645
damage: int = 10 # instance variable
641646
642647
ClassVar accepts only types and cannot be further subscribed.
@@ -769,7 +774,9 @@ def TypeAlias(self, parameters):
769774
770775
Use TypeAlias to indicate that an assignment should
771776
be recognized as a proper type alias definition by type
772-
checkers. For example::
777+
checkers.
778+
779+
For example::
773780
774781
Predicate: TypeAlias = Callable[..., bool]
775782
@@ -782,8 +789,8 @@ def TypeAlias(self, parameters):
782789
def Concatenate(self, parameters):
783790
"""Special form for annotating higher-order functions.
784791
785-
``Concatenate`` can be sed in conjunction with ``ParamSpec`` and
786-
``Callable`` to represent a higher order function which adds, removes or
792+
``Concatenate`` can be used in conjunction with ``ParamSpec`` and
793+
``Callable`` to represent a higher-order function which adds, removes or
787794
transforms the parameters of a callable.
788795
789796
For example::
@@ -1599,8 +1606,9 @@ def Unpack(self, parameters):
15991606
"""Type unpack operator.
16001607
16011608
The type unpack operator takes the child types from some container type,
1602-
such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'. For
1603-
example::
1609+
such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'.
1610+
1611+
For example::
16041612
16051613
# For some generic class `Foo`:
16061614
Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
@@ -1625,7 +1633,7 @@ class Bar(Generic[*Ts]): ...
16251633
class Bar[*Ts]: ...
16261634
16271635
The operator can also be used along with a `TypedDict` to annotate
1628-
`**kwargs` in a function signature. For instance::
1636+
`**kwargs` in a function signature::
16291637
16301638
class Movie(TypedDict):
16311639
name: str
@@ -1638,7 +1646,7 @@ def foo(**kwargs: Unpack[Movie]): ...
16381646
Note that there is only some runtime checking of this operator. Not
16391647
everything the runtime allows may be accepted by static type checkers.
16401648
1641-
For more information, see PEP 646.
1649+
For more information, see PEPs 646 and 692.
16421650
"""
16431651
item = _type_check(parameters, f'{self} accepts only single type.')
16441652
return _UnpackGenericAlias(origin=self, args=(item,))
@@ -1886,7 +1894,9 @@ def meth(self) -> int:
18861894
...
18871895
18881896
Such classes are primarily used with static type checkers that recognize
1889-
structural subtyping (static duck-typing), for example::
1897+
structural subtyping (static duck-typing).
1898+
1899+
For example::
18901900
18911901
class C:
18921902
def meth(self) -> int:
@@ -2043,7 +2053,7 @@ class Annotated:
20432053
20442054
Annotated[*Ts, Ann1] # NOT valid
20452055
2046-
This would be equivalent to
2056+
This would be equivalent to::
20472057
20482058
Annotated[T1, T2, T3, ..., Ann1]
20492059
@@ -2261,8 +2271,10 @@ def _strip_annotations(t):
22612271
def get_origin(tp):
22622272
"""Get the unsubscripted version of a type.
22632273
2264-
This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
2265-
Annotated, and others. Return None for unsupported types. Examples::
2274+
This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar,
2275+
Annotated, and others. Return None for unsupported types.
2276+
2277+
Examples::
22662278
22672279
assert get_origin(Literal[42]) is Literal
22682280
assert get_origin(int) is None
@@ -2421,7 +2433,9 @@ def overload(func):
24212433
"""Decorator for overloaded functions/methods.
24222434
24232435
In a stub file, place two or more stub definitions for the same
2424-
function in a row, each decorated with @overload. For example::
2436+
function in a row, each decorated with @overload.
2437+
2438+
For example::
24252439
24262440
@overload
24272441
def utf8(value: None) -> None: ...
@@ -2432,7 +2446,7 @@ def utf8(value: str) -> bytes: ...
24322446
24332447
In a non-stub file (i.e. a regular .py file), do the same but
24342448
follow it with an implementation. The implementation should *not*
2435-
be decorated with @overload. For example::
2449+
be decorated with @overload::
24362450
24372451
@overload
24382452
def utf8(value: None) -> None: ...
@@ -2942,7 +2956,9 @@ class Point2D(TypedDict):
29422956
def Required(self, parameters):
29432957
"""Special typing construct to mark a TypedDict key as required.
29442958
2945-
This is mainly useful for total=False TypedDicts. For example::
2959+
This is mainly useful for total=False TypedDicts.
2960+
2961+
For example::
29462962
29472963
class Movie(TypedDict, total=False):
29482964
title: Required[str]
@@ -2984,7 +3000,9 @@ class NewType:
29843000
29853001
NewType(name, tp) is considered a subtype of tp
29863002
by static type checkers. At runtime, NewType(name, tp) returns
2987-
a dummy callable that simply returns its argument. Usage::
3003+
a dummy callable that simply returns its argument.
3004+
3005+
Usage::
29883006
29893007
UserId = NewType('UserId', int)
29903008

0 commit comments

Comments
 (0)