Skip to content

Convert more examples to doctests in typing module #112194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sobolevn opened this issue Nov 17, 2023 · 0 comments
Closed

Convert more examples to doctests in typing module #112194

sobolevn opened this issue Nov 17, 2023 · 0 comments
Assignees
Labels
docs Documentation in the Doc dir tests Tests in the Lib/test dir topic-typing type-feature A feature request or enhancement

Comments

@sobolevn
Copy link
Member

sobolevn commented Nov 17, 2023

Feature or enhancement

There are multiple examples that are very similar to doctests, but are not doctests.
I propose adding >>> and ... to them, so these examples would be checked during tests (since now we have this feature).

There are some easy ones, where just adding >>> (and some imports are enough).
There are also some more complex ones, where some new types / vars are needed, but I don't think it is worth doing, because it will increase the complexity of these examples.

Examples:

  • cpython/Lib/typing.py

    Lines 207 to 217 in 0ee2d77

    def _should_unflatten_callable_args(typ, args):
    """Internal helper for munging collections.abc.Callable's __args__.
    The canonical representation for a Callable's __args__ flattens the
    argument types, see https://github.com/python/cpython/issues/86361.
    For example::
    assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
    assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
  • cpython/Lib/typing.py

    Lines 252 to 259 in 0ee2d77

    def _collect_parameters(args):
    """Collect all type variables and parameter specifications in args
    in order of first appearance (lexicographic order).
    For example::
    assert _collect_parameters((T, Callable[P, T])) == (T, P)
    """
  • cpython/Lib/typing.py

    Lines 672 to 686 in 0ee2d77

    - Unions of unions are flattened, e.g.::
    assert Union[Union[int, str], float] == Union[int, str, float]
    - Unions of a single argument vanish, e.g.::
    assert Union[int] == int # The constructor actually returns int
    - Redundant arguments are skipped, e.g.::
    assert Union[int, str, int] == Union[int, str]
    - When comparing unions, the argument order is ignored, e.g.::
    assert Union[int, str] == Union[str, int]
  • cpython/Lib/typing.py

    Lines 2239 to 2255 in 0ee2d77

    def get_origin(tp):
    """Get the unsubscripted version of a type.
    This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar,
    Annotated, and others. Return None for unsupported types.
    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
    """
  • cpython/Lib/typing.py

    Lines 2268 to 2280 in 0ee2d77

    def get_args(tp):
    """Get type arguments with all substitutions performed.
    For unions, basic simplifications used by Union constructor are performed.
    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)
    """
  • cpython/Lib/typing.py

    Lines 2293 to 2304 in 0ee2d77

    def is_typeddict(tp):
    """Check if an annotation is a TypedDict class.
    For example::
    class Film(TypedDict):
    title: str
    year: int
    is_typeddict(Film) # => True
    is_typeddict(Union[list, str]) # => False
    """
  • cpython/Lib/typing.py

    Lines 2899 to 2909 in 0ee2d77

    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')

This actually reveals a bug:

cpython/Lib/typing.py

Lines 207 to 219 in 0ee2d77

def _should_unflatten_callable_args(typ, args):
"""Internal helper for munging collections.abc.Callable's __args__.
The canonical representation for a Callable's __args__ flattens the
argument types, see https://github.com/python/cpython/issues/86361.
For example::
assert collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
As a result, if we need to reconstruct the Callable from its __args__,
we need to unflatten it.

assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str) example is invalid:

File "/Users/sobolev/Desktop/cpython2/Lib/typing.py", line 218, in typing._should_unflatten_callable_args
Failed example:
    assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
Exception raised:
    Traceback (most recent call last):
      File "/Users/sobolev/Desktop/cpython2/Lib/doctest.py", line 1374, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest typing._should_unflatten_callable_args[2]>", line 1, in <module>
        assert collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
               ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
      File "/Users/sobolev/Desktop/cpython2/Lib/_collections_abc.py", line 477, in __new__
        raise TypeError(f"Expected a list of types, an ellipsis, "
    TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'typing.ParamSpec'>

Linked PRs

@sobolevn sobolevn added type-feature A feature request or enhancement tests Tests in the Lib/test dir docs Documentation in the Doc dir topic-typing labels Nov 17, 2023
@sobolevn sobolevn self-assigned this Nov 17, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Nov 17, 2023
AlexWaygood added a commit that referenced this issue Nov 17, 2023
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 17, 2023
…thonGH-112195)

(cherry picked from commit 949b2cc)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 17, 2023
…thonGH-112195)

(cherry picked from commit 949b2cc)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this issue Nov 17, 2023
…H-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>
AlexWaygood added a commit that referenced this issue Nov 17, 2023
…H-112195) (#112208)

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>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…thon#112195)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
…thon#112195)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir tests Tests in the Lib/test dir topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants