@@ -212,8 +212,12 @@ def _should_unflatten_callable_args(typ, args):
212
212
213
213
For example::
214
214
215
- assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
216
- assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
215
+ >>> import collections.abc
216
+ >>> P = ParamSpec('P')
217
+ >>> collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
218
+ True
219
+ >>> collections.abc.Callable[P, str].__args__ == (P, str)
220
+ True
217
221
218
222
As a result, if we need to reconstruct the Callable from its __args__,
219
223
we need to unflatten it.
@@ -255,7 +259,10 @@ def _collect_parameters(args):
255
259
256
260
For example::
257
261
258
- assert _collect_parameters((T, Callable[P, T])) == (T, P)
262
+ >>> P = ParamSpec('P')
263
+ >>> T = TypeVar('T')
264
+ >>> _collect_parameters((T, Callable[P, T]))
265
+ (~T, ~P)
259
266
"""
260
267
parameters = []
261
268
for t in args :
@@ -2244,14 +2251,15 @@ def get_origin(tp):
2244
2251
2245
2252
Examples::
2246
2253
2247
- assert get_origin(Literal[42]) is Literal
2248
- assert get_origin(int) is None
2249
- assert get_origin(ClassVar[int]) is ClassVar
2250
- assert get_origin(Generic) is Generic
2251
- assert get_origin(Generic[T]) is Generic
2252
- assert get_origin(Union[T, int]) is Union
2253
- assert get_origin(List[Tuple[T, T]][int]) is list
2254
- assert get_origin(P.args) is P
2254
+ >>> P = ParamSpec('P')
2255
+ >>> assert get_origin(Literal[42]) is Literal
2256
+ >>> assert get_origin(int) is None
2257
+ >>> assert get_origin(ClassVar[int]) is ClassVar
2258
+ >>> assert get_origin(Generic) is Generic
2259
+ >>> assert get_origin(Generic[T]) is Generic
2260
+ >>> assert get_origin(Union[T, int]) is Union
2261
+ >>> assert get_origin(List[Tuple[T, T]][int]) is list
2262
+ >>> assert get_origin(P.args) is P
2255
2263
"""
2256
2264
if isinstance (tp , _AnnotatedAlias ):
2257
2265
return Annotated
@@ -2272,11 +2280,12 @@ def get_args(tp):
2272
2280
2273
2281
Examples::
2274
2282
2275
- assert get_args(Dict[str, int]) == (str, int)
2276
- assert get_args(int) == ()
2277
- assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
2278
- assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
2279
- assert get_args(Callable[[], T][int]) == ([], int)
2283
+ >>> T = TypeVar('T')
2284
+ >>> assert get_args(Dict[str, int]) == (str, int)
2285
+ >>> assert get_args(int) == ()
2286
+ >>> assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
2287
+ >>> assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
2288
+ >>> assert get_args(Callable[[], T][int]) == ([], int)
2280
2289
"""
2281
2290
if isinstance (tp , _AnnotatedAlias ):
2282
2291
return (tp .__origin__ ,) + tp .__metadata__
@@ -2295,12 +2304,15 @@ def is_typeddict(tp):
2295
2304
2296
2305
For example::
2297
2306
2298
- class Film(TypedDict):
2299
- title: str
2300
- year: int
2301
-
2302
- is_typeddict(Film) # => True
2303
- is_typeddict(Union[list, str]) # => False
2307
+ >>> from typing import TypedDict
2308
+ >>> class Film(TypedDict):
2309
+ ... title: str
2310
+ ... year: int
2311
+ ...
2312
+ >>> is_typeddict(Film)
2313
+ True
2314
+ >>> is_typeddict(dict)
2315
+ False
2304
2316
"""
2305
2317
return isinstance (tp , _TypedDictMeta )
2306
2318
@@ -2898,15 +2910,15 @@ def TypedDict(typename, fields=_sentinel, /, *, total=True):
2898
2910
2899
2911
Usage::
2900
2912
2901
- class Point2D(TypedDict):
2902
- x: int
2903
- y: int
2904
- label: str
2905
-
2906
- a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2907
- b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2908
-
2909
- assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2913
+ >>> class Point2D(TypedDict):
2914
+ ... x: int
2915
+ ... y: int
2916
+ ... label: str
2917
+ ...
2918
+ >>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2919
+ >>> b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2920
+ >>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2921
+ True
2910
2922
2911
2923
The type info can be accessed via the Point2D.__annotations__ dict, and
2912
2924
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
0 commit comments