Skip to content
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

Concatenate[str, bool, Q] spuriously duplicated to Concatenate[str, bool, str, bool, Q] #12734

Closed
andersk opened this issue May 5, 2022 · 2 comments · Fixed by #14677
Closed
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@andersk
Copy link
Contributor

andersk commented May 5, 2022

Mypy gives a false positive on this valid code. The error message seems to indicate that it’s spuriously duplicated the str, bool parameters to str, bool, str, bool somewhere.

from typing import Callable, Concatenate, ParamSpec

P = ParamSpec("P")
Q = ParamSpec("Q")

def foo(f: Callable[P, int]) -> Callable[P, int]:
    return f

def bar(f: Callable[Concatenate[str, bool, Q], int]) -> Callable[Concatenate[str, bool, Q], int]:
    return foo(f)

Mypy playground link

main.py:10: error: Incompatible return value type (got "Callable[[str, bool, str, bool, **Q], int]", expected "Callable[[str, bool, **Q], int]")
main.py:10: error: Argument 1 to "foo" has incompatible type "Callable[[str, bool, **Q], int]"; expected "Callable[[str, bool, str, bool, **Q], int]"
Found 2 errors in 1 file (checked 1 source file)
@andersk andersk added the bug mypy got something wrong label May 5, 2022
@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label May 5, 2022
@andersk
Copy link
Contributor Author

andersk commented May 6, 2022

Trying to eta-expand foo(f) results in other weird “incompatible type” errors—one where the LHS and RHS display as identical, and two where the RHS displays as a list of types.

from typing import Callable, Concatenate, ParamSpec

P = ParamSpec("P")
Q = ParamSpec("Q")

def foo(f: Callable[P, int]) -> Callable[P, int]:
    return f

def bar(f: Callable[Concatenate[str, bool, Q], int]) -> Callable[Concatenate[str, bool, Q], int]:
    def g(s: str, b: bool, *args: Q.args, **kwargs: Q.kwargs) -> int:
        return foo(f)(s, b, *args, **kwargs)

    return g
main.py:11: error: Argument 1 to "foo" has incompatible type "Callable[[str, bool, **Q], int]"; expected "Callable[[str, bool, **Q], int]"
main.py:11: error: Argument 1 has incompatible type "str"; expected "[str, bool, **Q.args]"
main.py:11: error: Argument 2 has incompatible type "bool"; expected "[str, bool, **Q.args]"
Found 3 errors in 1 file (checked 1 source file)

@PIG208
Copy link

PIG208 commented Jun 28, 2022

Relevant code where each ParamSpec argument eventually gets expanded twice

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.

3 participants