@@ -218,8 +218,12 @@ def _should_unflatten_callable_args(typ, args):
218
218
219
219
For example::
220
220
221
- assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
222
- assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
221
+ >>> import collections.abc
222
+ >>> P = ParamSpec('P')
223
+ >>> collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
224
+ True
225
+ >>> collections.abc.Callable[P, str].__args__ == (P, str)
226
+ True
223
227
224
228
As a result, if we need to reconstruct the Callable from its __args__,
225
229
we need to unflatten it.
@@ -261,7 +265,10 @@ def _collect_parameters(args):
261
265
262
266
For example::
263
267
264
- assert _collect_parameters((T, Callable[P, T])) == (T, P)
268
+ >>> P = ParamSpec('P')
269
+ >>> T = TypeVar('T')
270
+ >>> _collect_parameters((T, Callable[P, T]))
271
+ (~T, ~P)
265
272
"""
266
273
parameters = []
267
274
for t in args :
@@ -2268,14 +2275,15 @@ def get_origin(tp):
2268
2275
2269
2276
Examples::
2270
2277
2271
- assert get_origin(Literal[42]) is Literal
2272
- assert get_origin(int) is None
2273
- assert get_origin(ClassVar[int]) is ClassVar
2274
- assert get_origin(Generic) is Generic
2275
- assert get_origin(Generic[T]) is Generic
2276
- assert get_origin(Union[T, int]) is Union
2277
- assert get_origin(List[Tuple[T, T]][int]) is list
2278
- assert get_origin(P.args) is P
2278
+ >>> P = ParamSpec('P')
2279
+ >>> assert get_origin(Literal[42]) is Literal
2280
+ >>> assert get_origin(int) is None
2281
+ >>> assert get_origin(ClassVar[int]) is ClassVar
2282
+ >>> assert get_origin(Generic) is Generic
2283
+ >>> assert get_origin(Generic[T]) is Generic
2284
+ >>> assert get_origin(Union[T, int]) is Union
2285
+ >>> assert get_origin(List[Tuple[T, T]][int]) is list
2286
+ >>> assert get_origin(P.args) is P
2279
2287
"""
2280
2288
if isinstance (tp , _AnnotatedAlias ):
2281
2289
return Annotated
@@ -2296,11 +2304,12 @@ def get_args(tp):
2296
2304
2297
2305
Examples::
2298
2306
2299
- assert get_args(Dict[str, int]) == (str, int)
2300
- assert get_args(int) == ()
2301
- assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
2302
- assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
2303
- assert get_args(Callable[[], T][int]) == ([], int)
2307
+ >>> T = TypeVar('T')
2308
+ >>> assert get_args(Dict[str, int]) == (str, int)
2309
+ >>> assert get_args(int) == ()
2310
+ >>> assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
2311
+ >>> assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
2312
+ >>> assert get_args(Callable[[], T][int]) == ([], int)
2304
2313
"""
2305
2314
if isinstance (tp , _AnnotatedAlias ):
2306
2315
return (tp .__origin__ ,) + tp .__metadata__
@@ -2319,12 +2328,15 @@ def is_typeddict(tp):
2319
2328
2320
2329
For example::
2321
2330
2322
- class Film(TypedDict):
2323
- title: str
2324
- year: int
2325
-
2326
- is_typeddict(Film) # => True
2327
- is_typeddict(Union[list, str]) # => False
2331
+ >>> from typing import TypedDict
2332
+ >>> class Film(TypedDict):
2333
+ ... title: str
2334
+ ... year: int
2335
+ ...
2336
+ >>> is_typeddict(Film)
2337
+ True
2338
+ >>> is_typeddict(dict)
2339
+ False
2328
2340
"""
2329
2341
return isinstance (tp , _TypedDictMeta )
2330
2342
@@ -2881,15 +2893,15 @@ def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
2881
2893
2882
2894
Usage::
2883
2895
2884
- class Point2D(TypedDict):
2885
- x: int
2886
- y: int
2887
- label: str
2888
-
2889
- a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2890
- b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2891
-
2892
- assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2896
+ >>> class Point2D(TypedDict):
2897
+ ... x: int
2898
+ ... y: int
2899
+ ... label: str
2900
+ ...
2901
+ >>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2902
+ >>> b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2903
+ >>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2904
+ True
2893
2905
2894
2906
The type info can be accessed via the Point2D.__annotations__ dict, and
2895
2907
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
0 commit comments