summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-11-17 18:14:20 (GMT)
committerGitHub <noreply@github.com>2023-11-17 18:14:20 (GMT)
commite7aa40a3414b1834fb7aa891d76a81398a0ee828 (patch)
tree2f071d75dd52062132c4165fff2264080a48bfb8
parent7e4b66b86f7bc43760b4e413bb468b7f87168f0c (diff)
downloadcpython-e7aa40a3414b1834fb7aa891d76a81398a0ee828.zip
cpython-e7aa40a3414b1834fb7aa891d76a81398a0ee828.tar.gz
cpython-e7aa40a3414b1834fb7aa891d76a81398a0ee828.tar.bz2
[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 949b2cc6eae6ef4f3312dfd4e2650a138446fe77) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r--Lib/typing.py74
1 files changed, 43 insertions, 31 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 977e37d..fa812f9 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -211,8 +211,12 @@ def _should_unflatten_callable_args(typ, args):
For example::
- assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
- assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
+ >>> import collections.abc
+ >>> P = ParamSpec('P')
+ >>> collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
+ True
+ >>> collections.abc.Callable[P, str].__args__ == (P, str)
+ True
As a result, if we need to reconstruct the Callable from its __args__,
we need to unflatten it.
@@ -250,7 +254,10 @@ def _collect_parameters(args):
For example::
- assert _collect_parameters((T, Callable[P, T])) == (T, P)
+ >>> P = ParamSpec('P')
+ >>> T = TypeVar('T')
+ >>> _collect_parameters((T, Callable[P, T]))
+ (~T, ~P)
"""
parameters = []
for t in args:
@@ -2417,14 +2424,15 @@ def get_origin(tp):
Examples::
- assert get_origin(Literal[42]) is Literal
- assert get_origin(int) is None
- assert get_origin(ClassVar[int]) is ClassVar
- assert get_origin(Generic) is Generic
- assert get_origin(Generic[T]) is Generic
- assert get_origin(Union[T, int]) is Union
- assert get_origin(List[Tuple[T, T]][int]) is list
- assert get_origin(P.args) is P
+ >>> P = ParamSpec('P')
+ >>> assert get_origin(Literal[42]) is Literal
+ >>> assert get_origin(int) is None
+ >>> assert get_origin(ClassVar[int]) is ClassVar
+ >>> assert get_origin(Generic) is Generic
+ >>> assert get_origin(Generic[T]) is Generic
+ >>> assert get_origin(Union[T, int]) is Union
+ >>> assert get_origin(List[Tuple[T, T]][int]) is list
+ >>> assert get_origin(P.args) is P
"""
if isinstance(tp, _AnnotatedAlias):
return Annotated
@@ -2445,11 +2453,12 @@ def get_args(tp):
Examples::
- assert get_args(Dict[str, int]) == (str, int)
- assert get_args(int) == ()
- assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
- assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
- assert get_args(Callable[[], T][int]) == ([], int)
+ >>> T = TypeVar('T')
+ >>> assert get_args(Dict[str, int]) == (str, int)
+ >>> assert get_args(int) == ()
+ >>> assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
+ >>> assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
+ >>> assert get_args(Callable[[], T][int]) == ([], int)
"""
if isinstance(tp, _AnnotatedAlias):
return (tp.__origin__,) + tp.__metadata__
@@ -2468,12 +2477,15 @@ def is_typeddict(tp):
For example::
- class Film(TypedDict):
- title: str
- year: int
-
- is_typeddict(Film) # => True
- is_typeddict(Union[list, str]) # => False
+ >>> from typing import TypedDict
+ >>> class Film(TypedDict):
+ ... title: str
+ ... year: int
+ ...
+ >>> is_typeddict(Film)
+ True
+ >>> is_typeddict(dict)
+ False
"""
return isinstance(tp, _TypedDictMeta)
@@ -3022,15 +3034,15 @@ def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Usage::
- class Point2D(TypedDict):
- x: int
- y: int
- label: str
-
- a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
- b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
-
- assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
+ >>> class Point2D(TypedDict):
+ ... x: int
+ ... y: int
+ ... label: str
+ ...
+ >>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
+ >>> b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
+ >>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
+ True
The type info can be accessed via the Point2D.__annotations__ dict, and
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.