Skip to content

Commit 949b2cc

Browse files
gh-112194: Convert more examples to doctests in typing.py (#112195)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 9fb0f2d commit 949b2cc

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

Lib/typing.py

+43-31
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,12 @@ def _should_unflatten_callable_args(typ, args):
212212
213213
For example::
214214
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
217221
218222
As a result, if we need to reconstruct the Callable from its __args__,
219223
we need to unflatten it.
@@ -255,7 +259,10 @@ def _collect_parameters(args):
255259
256260
For example::
257261
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)
259266
"""
260267
parameters = []
261268
for t in args:
@@ -2244,14 +2251,15 @@ def get_origin(tp):
22442251
22452252
Examples::
22462253
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
22552263
"""
22562264
if isinstance(tp, _AnnotatedAlias):
22572265
return Annotated
@@ -2272,11 +2280,12 @@ def get_args(tp):
22722280
22732281
Examples::
22742282
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)
22802289
"""
22812290
if isinstance(tp, _AnnotatedAlias):
22822291
return (tp.__origin__,) + tp.__metadata__
@@ -2295,12 +2304,15 @@ def is_typeddict(tp):
22952304
22962305
For example::
22972306
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
23042316
"""
23052317
return isinstance(tp, _TypedDictMeta)
23062318

@@ -2898,15 +2910,15 @@ def TypedDict(typename, fields=_sentinel, /, *, total=True):
28982910
28992911
Usage::
29002912
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
29102922
29112923
The type info can be accessed via the Point2D.__annotations__ dict, and
29122924
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.

0 commit comments

Comments
 (0)