Skip to content

Can't seem to type a decorator that works with ParamSpec on methods #16333

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
Dreamsorcerer opened this issue Oct 26, 2023 · 3 comments
Closed
Labels
bug mypy got something wrong

Comments

@Dreamsorcerer
Copy link
Contributor

I've been struggling with this one, and I'm not sure if I'm doing something wrong or mypy is doing something wrong:

from typing import Callable, Generic, ParamSpec, TypeVar

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

class A(Generic[P, R]):
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...

def decorator(fn: Callable[P, R]) -> A[P, R]: ...

class B:
    @decorator
    def foo(self) -> None: ...


b = B()
b.foo()  # error: Missing positional argument "self" in call to "__call__" of "A"  [call-arg]

I need the decorator, which returns the A wrapper, to work with both functions and methods.

Full project is at: aio-libs/async-lru#508

@Dreamsorcerer Dreamsorcerer added the bug mypy got something wrong label Oct 26, 2023
@brandonchinn178
Copy link

What's the purpose of A? Can't you just do

def decorator(fn: Callable[P, R]) -> Callable[P, R]:
    ...

?

@Dreamsorcerer
Copy link
Contributor Author

See the link in the description. A in the actual project is called _LRUCacheWrapper.

@Dreamsorcerer
Copy link
Contributor Author

Anyway I think I ended up narrowing down the cause of my problem to some awkward method in the implementation, so I don't think this is a real issue anymore.

@Dreamsorcerer Dreamsorcerer closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants