@@ -211,8 +211,12 @@ def _should_unflatten_callable_args(typ, args):
211
211
212
212
For example::
213
213
214
- assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
215
- assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
214
+ >>> import collections.abc
215
+ >>> P = ParamSpec('P')
216
+ >>> collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
217
+ True
218
+ >>> collections.abc.Callable[P, str].__args__ == (P, str)
219
+ True
216
220
217
221
As a result, if we need to reconstruct the Callable from its __args__,
218
222
we need to unflatten it.
@@ -250,7 +254,10 @@ def _collect_parameters(args):
250
254
251
255
For example::
252
256
253
- assert _collect_parameters((T, Callable[P, T])) == (T, P)
257
+ >>> P = ParamSpec('P')
258
+ >>> T = TypeVar('T')
259
+ >>> _collect_parameters((T, Callable[P, T]))
260
+ (~T, ~P)
254
261
"""
255
262
parameters = []
256
263
for t in args :
@@ -2417,14 +2424,15 @@ def get_origin(tp):
2417
2424
2418
2425
Examples::
2419
2426
2420
- assert get_origin(Literal[42]) is Literal
2421
- assert get_origin(int) is None
2422
- assert get_origin(ClassVar[int]) is ClassVar
2423
- assert get_origin(Generic) is Generic
2424
- assert get_origin(Generic[T]) is Generic
2425
- assert get_origin(Union[T, int]) is Union
2426
- assert get_origin(List[Tuple[T, T]][int]) is list
2427
- assert get_origin(P.args) is P
2427
+ >>> P = ParamSpec('P')
2428
+ >>> assert get_origin(Literal[42]) is Literal
2429
+ >>> assert get_origin(int) is None
2430
+ >>> assert get_origin(ClassVar[int]) is ClassVar
2431
+ >>> assert get_origin(Generic) is Generic
2432
+ >>> assert get_origin(Generic[T]) is Generic
2433
+ >>> assert get_origin(Union[T, int]) is Union
2434
+ >>> assert get_origin(List[Tuple[T, T]][int]) is list
2435
+ >>> assert get_origin(P.args) is P
2428
2436
"""
2429
2437
if isinstance (tp , _AnnotatedAlias ):
2430
2438
return Annotated
@@ -2445,11 +2453,12 @@ def get_args(tp):
2445
2453
2446
2454
Examples::
2447
2455
2448
- assert get_args(Dict[str, int]) == (str, int)
2449
- assert get_args(int) == ()
2450
- assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
2451
- assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
2452
- assert get_args(Callable[[], T][int]) == ([], int)
2456
+ >>> T = TypeVar('T')
2457
+ >>> assert get_args(Dict[str, int]) == (str, int)
2458
+ >>> assert get_args(int) == ()
2459
+ >>> assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
2460
+ >>> assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
2461
+ >>> assert get_args(Callable[[], T][int]) == ([], int)
2453
2462
"""
2454
2463
if isinstance (tp , _AnnotatedAlias ):
2455
2464
return (tp .__origin__ ,) + tp .__metadata__
@@ -2468,12 +2477,15 @@ def is_typeddict(tp):
2468
2477
2469
2478
For example::
2470
2479
2471
- class Film(TypedDict):
2472
- title: str
2473
- year: int
2474
-
2475
- is_typeddict(Film) # => True
2476
- is_typeddict(Union[list, str]) # => False
2480
+ >>> from typing import TypedDict
2481
+ >>> class Film(TypedDict):
2482
+ ... title: str
2483
+ ... year: int
2484
+ ...
2485
+ >>> is_typeddict(Film)
2486
+ True
2487
+ >>> is_typeddict(dict)
2488
+ False
2477
2489
"""
2478
2490
return isinstance (tp , _TypedDictMeta )
2479
2491
@@ -3022,15 +3034,15 @@ def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
3022
3034
3023
3035
Usage::
3024
3036
3025
- class Point2D(TypedDict):
3026
- x: int
3027
- y: int
3028
- label: str
3029
-
3030
- a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
3031
- b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
3032
-
3033
- assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
3037
+ >>> class Point2D(TypedDict):
3038
+ ... x: int
3039
+ ... y: int
3040
+ ... label: str
3041
+ ...
3042
+ >>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
3043
+ >>> b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
3044
+ >>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
3045
+ True
3034
3046
3035
3047
The type info can be accessed via the Point2D.__annotations__ dict, and
3036
3048
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
0 commit comments