Skip to content

ParamspecArgs cannot be used with enumerate #12930

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
kasium opened this issue Jun 2, 2022 · 3 comments · Fixed by #12938
Closed

ParamspecArgs cannot be used with enumerate #12930

kasium opened this issue Jun 2, 2022 · 3 comments · Fixed by #12938
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@kasium
Copy link

kasium commented Jun 2, 2022

Bug Report

With #12386 an issue was closed, that ParamSpec cannot be used as tuples and dicts. However, enumerate is seen as invalid, even if it works.

To Reproduce

from typing_extensions import ParamSpec
from typing import Callable, TypeVar

P = ParamSpec("P")
R = TypeVar("R")

def foo(func: Callable[P, R]) -> Callable[P, R]:
    def _inner(*args: P.args, **kwargs: P.kwargs) -> R:
        x = kwargs.pop("x")
        y = enumerate(args)
        return func(*args, **kwargs)
    return _inner

Expected Behavior

It works fine

Actual Behavior

foo.py:10:13: error: Need type annotation for "y"  [var-annotated]
            y = enumerate(args)
                ^
foo.py:10:23: error: Argument 1 to "enumerate" has incompatible type "P.args"; expected "Iterable[<nothing>]"  [arg-type]
            y = enumerate(args)
                          ^
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.960
  • Python version used: 3.7.2
  • Operating system and version: SUSE Linux
@kasium kasium added the bug mypy got something wrong label Jun 2, 2022
@AlexWaygood
Copy link
Member

This looks like a bug to me, but here's a workaround in the meantime, that passes mypy:

from typing_extensions import ParamSpec
from typing import Callable, TypeVar

P = ParamSpec("P")
R = TypeVar("R")

def foo(func: Callable[P, R]) -> Callable[P, R]:
    def _inner(*args: P.args, **kwargs: P.kwargs) -> R:
        x = kwargs.pop("x")
        y: "enumerate[object]" = enumerate(args)
        return func(*args, **kwargs)
    return _inner

@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Jun 2, 2022
@kasium
Copy link
Author

kasium commented Jun 2, 2022

@AlexWaygood thanks!

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 4, 2022

Here's a repro that doesn't depend on the typeshed stubs for enumerate:

from typing_extensions import ParamSpec
from typing import Callable, TypeVar, Iterator, Iterable, Tuple

P = ParamSpec("P")
T = TypeVar("T")

def enumerate(x: Iterable[T]) -> Iterator[Tuple[int, T]]: ...

def foo(func: Callable[P, T]) -> Callable[P, T]:
    def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
        x = kwargs.pop("x")
        y = enumerate(args)  # E: Need type annotation for "y"  # E: Argument 1 to "enumerate" has incompatible type "P.args"; expected "Iterable[<nothing>]"
        return func(*args, **kwargs)
    return _inner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants