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.
16
13
SupportsFloat, SupportsIndex, SupportsAbs, and others.
17
14
* Special types: NewType, NamedTuple, TypedDict.
18
15
* 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!
19
19
"""
20
20
21
21
from abc import abstractmethod , ABCMeta
@@ -208,10 +208,12 @@ def _should_unflatten_callable_args(typ, args):
208
208
"""Internal helper for munging collections.abc.Callable's __args__.
209
209
210
210
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.
212
212
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)
215
217
216
218
As a result, if we need to reconstruct the Callable from its __args__,
217
219
we need to unflatten it.
@@ -345,8 +347,9 @@ def _flatten_literal_params(parameters):
345
347
346
348
347
349
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.
350
353
"""
351
354
def decorator (func ):
352
355
# The callback 'inner' references the newly created lru_cache
@@ -627,10 +630,12 @@ def ClassVar(self, parameters):
627
630
628
631
An annotation wrapped in ClassVar indicates that a given
629
632
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::
631
636
632
637
class Starship:
633
- stats: ClassVar[Dict [str, int]] = {} # class variable
638
+ stats: ClassVar[dict [str, int]] = {} # class variable
634
639
damage: int = 10 # instance variable
635
640
636
641
ClassVar accepts only types and cannot be further subscribed.
@@ -763,7 +768,9 @@ def TypeAlias(self, parameters):
763
768
764
769
Use TypeAlias to indicate that an assignment should
765
770
be recognized as a proper type alias definition by type
766
- checkers. For example::
771
+ checkers.
772
+
773
+ For example::
767
774
768
775
Predicate: TypeAlias = Callable[..., bool]
769
776
@@ -776,8 +783,8 @@ def TypeAlias(self, parameters):
776
783
def Concatenate (self , parameters ):
777
784
"""Special form for annotating higher-order functions.
778
785
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
781
788
transforms the parameters of a callable.
782
789
783
790
For example::
@@ -1593,8 +1600,9 @@ def Unpack(self, parameters):
1593
1600
"""Type unpack operator.
1594
1601
1595
1602
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::
1598
1606
1599
1607
# For some generic class `Foo`:
1600
1608
Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
@@ -1619,7 +1627,7 @@ class Bar(Generic[*Ts]): ...
1619
1627
class Bar[*Ts]: ...
1620
1628
1621
1629
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::
1623
1631
1624
1632
class Movie(TypedDict):
1625
1633
name: str
@@ -1632,7 +1640,7 @@ def foo(**kwargs: Unpack[Movie]): ...
1632
1640
Note that there is only some runtime checking of this operator. Not
1633
1641
everything the runtime allows may be accepted by static type checkers.
1634
1642
1635
- For more information, see PEP 646.
1643
+ For more information, see PEPs 646 and 692 .
1636
1644
"""
1637
1645
item = _type_check (parameters , f'{ self } accepts only single type.' )
1638
1646
return _UnpackGenericAlias (origin = self , args = (item ,))
@@ -1880,7 +1888,9 @@ def meth(self) -> int:
1880
1888
...
1881
1889
1882
1890
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::
1884
1894
1885
1895
class C:
1886
1896
def meth(self) -> int:
@@ -2037,7 +2047,7 @@ class Annotated:
2037
2047
2038
2048
Annotated[*Ts, Ann1] # NOT valid
2039
2049
2040
- This would be equivalent to
2050
+ This would be equivalent to::
2041
2051
2042
2052
Annotated[T1, T2, T3, ..., Ann1]
2043
2053
@@ -2255,8 +2265,10 @@ def _strip_annotations(t):
2255
2265
def get_origin (tp ):
2256
2266
"""Get the unsubscripted version of a type.
2257
2267
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::
2260
2272
2261
2273
assert get_origin(Literal[42]) is Literal
2262
2274
assert get_origin(int) is None
@@ -2415,7 +2427,9 @@ def overload(func):
2415
2427
"""Decorator for overloaded functions/methods.
2416
2428
2417
2429
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::
2419
2433
2420
2434
@overload
2421
2435
def utf8(value: None) -> None: ...
@@ -2426,7 +2440,7 @@ def utf8(value: str) -> bytes: ...
2426
2440
2427
2441
In a non-stub file (i.e. a regular .py file), do the same but
2428
2442
follow it with an implementation. The implementation should *not*
2429
- be decorated with @overload. For example ::
2443
+ be decorated with @overload::
2430
2444
2431
2445
@overload
2432
2446
def utf8(value: None) -> None: ...
@@ -2925,7 +2939,9 @@ class Point2D(TypedDict):
2925
2939
def Required (self , parameters ):
2926
2940
"""Special typing construct to mark a TypedDict key as required.
2927
2941
2928
- This is mainly useful for total=False TypedDicts. For example::
2942
+ This is mainly useful for total=False TypedDicts.
2943
+
2944
+ For example::
2929
2945
2930
2946
class Movie(TypedDict, total=False):
2931
2947
title: Required[str]
@@ -2967,7 +2983,9 @@ class NewType:
2967
2983
2968
2984
NewType(name, tp) is considered a subtype of tp
2969
2985
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::
2971
2989
2972
2990
UserId = NewType('UserId', int)
2973
2991
0 commit comments