1
1
"""
2
2
The typing module: Support for gradual typing as defined by PEP 484 and subsequent PEPs.
3
3
4
- Any name not present in __all__ is an implementation detail
5
- that may be changed without notice. Use at your own risk!
6
-
7
4
Among other things, the module includes the following:
8
5
* Generic, Protocol, and internal machinery to support generic aliases.
9
6
All subscripted types like X[int], Union[int, str] are generic aliases.
17
14
* Special types: NewType, NamedTuple, TypedDict.
18
15
* Deprecated wrapper submodules for re and io related types.
19
16
* 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!
20
20
"""
21
21
22
22
from abc import abstractmethod , ABCMeta
@@ -207,10 +207,12 @@ def _should_unflatten_callable_args(typ, args):
207
207
"""Internal helper for munging collections.abc.Callable's __args__.
208
208
209
209
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.
211
211
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)
214
216
215
217
As a result, if we need to reconstruct the Callable from its __args__,
216
218
we need to unflatten it.
@@ -339,8 +341,9 @@ def _flatten_literal_params(parameters):
339
341
340
342
341
343
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.
344
347
"""
345
348
def decorator (func ):
346
349
cached = functools .lru_cache (typed = typed )(func )
@@ -614,10 +617,12 @@ def ClassVar(self, parameters):
614
617
615
618
An annotation wrapped in ClassVar indicates that a given
616
619
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::
618
623
619
624
class Starship:
620
- stats: ClassVar[Dict [str, int]] = {} # class variable
625
+ stats: ClassVar[dict [str, int]] = {} # class variable
621
626
damage: int = 10 # instance variable
622
627
623
628
ClassVar accepts only types and cannot be further subscribed.
@@ -741,7 +746,9 @@ def TypeAlias(self, parameters):
741
746
742
747
Use TypeAlias to indicate that an assignment should
743
748
be recognized as a proper type alias definition by type
744
- checkers. For example::
749
+ checkers.
750
+
751
+ For example::
745
752
746
753
Predicate: TypeAlias = Callable[..., bool]
747
754
@@ -754,8 +761,8 @@ def TypeAlias(self, parameters):
754
761
def Concatenate (self , parameters ):
755
762
"""Special form for annotating higher-order functions.
756
763
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
759
766
transforms the parameters of a callable.
760
767
761
768
For example::
@@ -1713,8 +1720,9 @@ def Unpack(self, parameters):
1713
1720
"""Type unpack operator.
1714
1721
1715
1722
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::
1718
1726
1719
1727
# For some generic class `Foo`:
1720
1728
Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
@@ -2007,7 +2015,9 @@ def meth(self) -> int:
2007
2015
...
2008
2016
2009
2017
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::
2011
2021
2012
2022
class C:
2013
2023
def meth(self) -> int:
@@ -2184,7 +2194,7 @@ class Annotated:
2184
2194
2185
2195
Annotated[*Ts, Ann1] # NOT valid
2186
2196
2187
- This would be equivalent to
2197
+ This would be equivalent to::
2188
2198
2189
2199
Annotated[T1, T2, T3, ..., Ann1]
2190
2200
@@ -2402,8 +2412,10 @@ def _strip_annotations(t):
2402
2412
def get_origin (tp ):
2403
2413
"""Get the unsubscripted version of a type.
2404
2414
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::
2407
2419
2408
2420
assert get_origin(Literal[42]) is Literal
2409
2421
assert get_origin(int) is None
@@ -2562,7 +2574,9 @@ def overload(func):
2562
2574
"""Decorator for overloaded functions/methods.
2563
2575
2564
2576
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::
2566
2580
2567
2581
@overload
2568
2582
def utf8(value: None) -> None: ...
@@ -2573,7 +2587,7 @@ def utf8(value: str) -> bytes: ...
2573
2587
2574
2588
In a non-stub file (i.e. a regular .py file), do the same but
2575
2589
follow it with an implementation. The implementation should *not*
2576
- be decorated with @overload. For example ::
2590
+ be decorated with @overload::
2577
2591
2578
2592
@overload
2579
2593
def utf8(value: None) -> None: ...
@@ -3075,7 +3089,9 @@ class Point2D(TypedDict):
3075
3089
def Required (self , parameters ):
3076
3090
"""Special typing construct to mark a TypedDict key as required.
3077
3091
3078
- This is mainly useful for total=False TypedDicts. For example::
3092
+ This is mainly useful for total=False TypedDicts.
3093
+
3094
+ For example::
3079
3095
3080
3096
class Movie(TypedDict, total=False):
3081
3097
title: Required[str]
@@ -3117,7 +3133,9 @@ class NewType:
3117
3133
3118
3134
NewType(name, tp) is considered a subtype of tp
3119
3135
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::
3121
3139
3122
3140
UserId = NewType('UserId', int)
3123
3141
0 commit comments