mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[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 949b2cc6ea
)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
976488ebf6
commit
0ea645445d
1 changed files with 43 additions and 31 deletions
|
@ -218,8 +218,12 @@ def _should_unflatten_callable_args(typ, args):
|
||||||
|
|
||||||
For example::
|
For example::
|
||||||
|
|
||||||
assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
|
>>> import collections.abc
|
||||||
assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
|
>>> 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__,
|
As a result, if we need to reconstruct the Callable from its __args__,
|
||||||
we need to unflatten it.
|
we need to unflatten it.
|
||||||
|
@ -261,7 +265,10 @@ def _collect_parameters(args):
|
||||||
|
|
||||||
For example::
|
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 = []
|
parameters = []
|
||||||
for t in args:
|
for t in args:
|
||||||
|
@ -2268,14 +2275,15 @@ def get_origin(tp):
|
||||||
|
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
assert get_origin(Literal[42]) is Literal
|
>>> P = ParamSpec('P')
|
||||||
assert get_origin(int) is None
|
>>> assert get_origin(Literal[42]) is Literal
|
||||||
assert get_origin(ClassVar[int]) is ClassVar
|
>>> assert get_origin(int) is None
|
||||||
assert get_origin(Generic) is Generic
|
>>> assert get_origin(ClassVar[int]) is ClassVar
|
||||||
assert get_origin(Generic[T]) is Generic
|
>>> assert get_origin(Generic) is Generic
|
||||||
assert get_origin(Union[T, int]) is Union
|
>>> assert get_origin(Generic[T]) is Generic
|
||||||
assert get_origin(List[Tuple[T, T]][int]) is list
|
>>> assert get_origin(Union[T, int]) is Union
|
||||||
assert get_origin(P.args) is P
|
>>> assert get_origin(List[Tuple[T, T]][int]) is list
|
||||||
|
>>> assert get_origin(P.args) is P
|
||||||
"""
|
"""
|
||||||
if isinstance(tp, _AnnotatedAlias):
|
if isinstance(tp, _AnnotatedAlias):
|
||||||
return Annotated
|
return Annotated
|
||||||
|
@ -2296,11 +2304,12 @@ def get_args(tp):
|
||||||
|
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
assert get_args(Dict[str, int]) == (str, int)
|
>>> T = TypeVar('T')
|
||||||
assert get_args(int) == ()
|
>>> assert get_args(Dict[str, int]) == (str, int)
|
||||||
assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
|
>>> assert get_args(int) == ()
|
||||||
assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
|
>>> assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
|
||||||
assert get_args(Callable[[], T][int]) == ([], int)
|
>>> assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
|
||||||
|
>>> assert get_args(Callable[[], T][int]) == ([], int)
|
||||||
"""
|
"""
|
||||||
if isinstance(tp, _AnnotatedAlias):
|
if isinstance(tp, _AnnotatedAlias):
|
||||||
return (tp.__origin__,) + tp.__metadata__
|
return (tp.__origin__,) + tp.__metadata__
|
||||||
|
@ -2319,12 +2328,15 @@ def is_typeddict(tp):
|
||||||
|
|
||||||
For example::
|
For example::
|
||||||
|
|
||||||
class Film(TypedDict):
|
>>> from typing import TypedDict
|
||||||
title: str
|
>>> class Film(TypedDict):
|
||||||
year: int
|
... title: str
|
||||||
|
... year: int
|
||||||
is_typeddict(Film) # => True
|
...
|
||||||
is_typeddict(Union[list, str]) # => False
|
>>> is_typeddict(Film)
|
||||||
|
True
|
||||||
|
>>> is_typeddict(dict)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
return isinstance(tp, _TypedDictMeta)
|
return isinstance(tp, _TypedDictMeta)
|
||||||
|
|
||||||
|
@ -2881,15 +2893,15 @@ def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
|
||||||
|
|
||||||
Usage::
|
Usage::
|
||||||
|
|
||||||
class Point2D(TypedDict):
|
>>> class Point2D(TypedDict):
|
||||||
x: int
|
... x: int
|
||||||
y: int
|
... y: int
|
||||||
label: str
|
... label: str
|
||||||
|
...
|
||||||
a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
|
>>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
|
||||||
b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
|
>>> b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
|
||||||
|
>>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
|
||||||
assert 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 type info can be accessed via the Point2D.__annotations__ dict, and
|
||||||
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
|
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue