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

ParamSpec parameters don't match __init__ parameters of class #12986

Closed
NeilGirdhar opened this issue Jun 15, 2022 · 3 comments
Closed

ParamSpec parameters don't match __init__ parameters of class #12986

NeilGirdhar opened this issue Jun 15, 2022 · 3 comments
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@NeilGirdhar
Copy link
Contributor

from functools import partial
from typing import Callable, Generic, ParamSpec

P = ParamSpec('P')

class custom_vjp(Generic[P]):
    def __init__(self,
                 fun: Callable[P, None],
                 x: int = 0):
        super().__init__()
        self.fun = fun

    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> None:
        return self.fun(*args, **kwargs)

@partial(custom_vjp, x=1)  # error: Argument 1 to "partial" has incompatible type "Type[custom_vjp[Any]]"; expected "Callable[..., custom_vjp[P]]"
def f() -> None:
    pass
@NeilGirdhar NeilGirdhar added the bug mypy got something wrong label Jun 15, 2022
@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Jun 15, 2022
@kstauffer
Copy link
Contributor

You get the same error without the decorator (delete the @ character and the last two lines entirely). The error is at the instantiation of the partial object.

The problem is the use of an unbound type in a way that mypy cannot handle. Where the error message says "Any" is misleading; the internal state of mypy still has "P" unbound. Notice that mypy is happy if you explicitly annotate or cast with the same dubious type mypy already claims to think it has:

con: Type[custom_vjp[Any]] = custom_vjp
partial(con, x=1)  # no issues found
partial(cast(Type[custom_vjp[Any]], custom_vjp), x=1)  # no issues found

That's proof that the mypy error message is not really telling you what's gone wrong inside mypy.

I am working on a plugin to properly handle functools.partial and partialmethod. I'll be sure this handle this test case.

@NeilGirdhar
Copy link
Contributor Author

Awesome!! Really looking forward to your plugin. And thank you for diagnosing the error and providing a workaround.

@ilevkivskyi
Copy link
Member

The original example works on master, likely fixed by #15837

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