8000 [3.11] gh-112194: Convert more examples to doctests in `typing.py` (G… · python/cpython@e7aa40a · GitHub
[go: up one dir, main page]

Skip to content

Commit e7aa40a

Browse files
miss-islingtonsobolevnAlexWaygood
authored
[3.11] gh-112194: Convert more examples to doctests in typing.py (GH-112195) (#112209)
gh-112194: Convert more examples to doctests in `typing.py` (GH-112195) (cherry picked from commit 949b2cc) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 7e4b66b commit e7aa40a

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

Lib/typing.py

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,12 @@ def _should_unflatten_callable_args(typ, args):
211211
212212
For example::
213213
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
216220
217221
As a result, if we need to reconstruct the Callable from its __args__,
218222
we need to unflatten it.
@@ -250,7 +254,10 @@ def _collect_parameters(args):
250254
251255
For example::
252256
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)
254261
"""
255262
parameters = []
256263
for t in args:
@@ -2417,14 +2424,15 @@ def get_origin(tp):
24172424
24182425
Examples::
24192426
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
24282436
"""
24292437
if isinstance(tp, _AnnotatedAlias):
24302438
return Annotated
@@ -2445,11 +2453,12 @@ def get_args(tp):
24452453
24462454
Examples::
24472455
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)
24532462
"""
24542463
if isinstance(tp, _AnnotatedAlias):
24552464
return (tp.__origin__,) + tp.__metadata__
@@ -2468,12 +2477,15 @@ def is_typeddict(tp):
24682477
24692478
For example::
24702479
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
24772489
"""
24782490
return isinstance(tp, _TypedDictMeta)
24792491

@@ -3022,15 +3034,15 @@ def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
30223034
30233035
Usage::
30243036
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
30343046
30353047
The type info can be accessed via the Point2D.__annotations__ dict, and
30363048
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.

0 commit comments

Comments
 (0)
0