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

no error when calling Callable with ParamSpec using Concatenate with not enough args #14571

Open
DetachHead opened this issue Feb 1, 2023 · 6 comments · May be fixed by #17323
Open

no error when calling Callable with ParamSpec using Concatenate with not enough args #14571

DetachHead opened this issue Feb 1, 2023 · 6 comments · May be fixed by #17323
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Feb 1, 2023

from typing import Callable, Concatenate, ParamSpec

P = ParamSpec("P")


def decorator(fn: Callable[Concatenate[str, P], None]) -> None:
    fn("value") # runtime TypeError: foo() missing 1 required positional argument: 's2'


@decorator
def foo(s: str, s2: str) -> None:
    ...

playground

@DetachHead DetachHead added the bug mypy got something wrong label Feb 1, 2023
@AlexWaygood
Copy link
Member

I think mypy should indeed emit an error on your code snippet, but not the error you think. Your use of ParamSpec here is invalid -- the ParamSpec P in the decorator function will go unsolved, since it is only used once in the function signature.

@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Feb 1, 2023
@KotlinIsland
Copy link
Contributor

KotlinIsland commented Feb 1, 2023

@AlexWaygood It is true that the ParamSpec is only used once, but there is still an issue that the P could represent any signature. Look at this playground

@AlexWaygood
Copy link
Member

AlexWaygood commented Feb 1, 2023

@AlexWaygood It is true that the ParamSpec is only used once, but there is still an issue that the P could represent any signature. Look at this playground

Yes, that example illustrates the type-safety issue much more clearly. So that other people do not have to click through to see the snippet, here it is copied out. No mypy errors are emitted, but TypeError will always be raised if you try to call the foo function at runtime:

from typing import Callable, Concatenate, ParamSpec

P = ParamSpec("P")


def decorator(fn: Callable[Concatenate[str, P], None]) -> Callable[P, None]:
    def inner(*args: P.args, **kwargs: P.kwargs) -> None:
        fn("value")
    return inner
        


@decorator
def foo(s: str, s2: str) -> None:
    ...

@sterliakov
Copy link
Contributor

Seems like this has nothing to do with Concatenate. The following is green on master, with and without --strict:

from typing_extensions import ParamSpec
from typing import Callable

_P = ParamSpec("_P")

def run(predicate: Callable[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None:
    predicate()

@sterliakov
Copy link
Contributor

@AlexWaygood Since this is a major problem (not detecting a relatively common problem of forgetting to pass args&kwargs), I'll try to ping someone here once again: are there any blockers that prevent #17323 (green, small, mergeable) from being merged? Is there anyone I can ping to get that patch reviewed? Something wrong there from my side, making the review or the patch implausible/undesired/inefficient/whatever?

@AlexWaygood
Copy link
Member

Unfortunately I'm only a triager here so do not have power to merge. I could review, but I also have pretty limited time to work on mypy right now, sadly. I'll post on the mypy maintainer Discord, however

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.

4 participants