Skip to content

Commit 0ea6454

Browse files
miss-islingtonsobolevnAlexWaygood
authored
[3.12] gh-112194: Convert more examples to doctests in typing.py (GH-112195) (#112208)
gh-112194: Convert more examples to doctests in `typing.py` (GH-112195) (cherry picked from commit 949b2cc) Co-authored-by: Nikita Sobolev <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent 976488e commit 0ea6454

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
@@ -218,8 +218,12 @@ def _should_unflatten_callable_args(typ, args):
218218
219219
For example::
220220
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
223227
224228
As a result, if we need to reconstruct the Callable from its __args__,
225229
we need to unflatten it.
@@ -261,7 +265,10 @@ def _collect_parameters(args):
261265
262266
For example::
263267
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)
265272
"""
266273
parameters = []
267274
for t in args:
@@ -2268,14 +2275,15 @@ def get_origin(tp):
22682275
22692276
Examples::
22702277
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
22792287
"""
22802288
if isinstance(tp, _AnnotatedAlias):
22812289
return Annotated
@@ -2296,11 +2304,12 @@ def get_args(tp):
22962304
22972305
Examples::
22982306
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)
23042313
"""
23052314
if isinstance(tp, _AnnotatedAlias):
23062315
return (tp.__origin__,) + tp.__metadata__
@@ -2319,12 +2328,15 @@ def is_typeddict(tp):
23192328
23202329
For example::
23212330
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
23282340
"""
23292341
return isinstance(tp, _TypedDictMeta)
23302342

@@ -2881,15 +2893,15 @@ def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
28812893
28822894
Usage::
28832895
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
28932905
28942906
The type info can be accessed via the Point2D.__annotations__ dict, and
28952907
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.

0 commit comments

Comments
 (0)