-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 + Generics doesn't work #14968
Comments
Your sample doesn't define the symbol |
@erictraut sorry, there was a small mistype. I've updated the example. |
I agree this is a bug in mypy, but you're using class Example(Generic[P]):
def __init__(self, fn: Callable[P, Any]):
self.fn = fn
def __call__(self, *args: P.args, **kwargs: P.kwargs):
... |
@erictraut Real examples are more complex and I need whole callable object as TypeVar for the Generic. But you gave me some ideas for further research. Thank you! 🙏 |
You probably already figured this out, but if you need the whole callable, you can also capture the return type in the constructor. P = ParamSpec("P")
R = TypeVar("R")
class Example(Generic[P, R]):
def __init__(self, fn: Callable[P, R]):
self.fn = fn
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
... One situation where this approach won't work (and your original example will) is if the captured function is overloaded. |
Fixes #14968 Fixes #13911 The fix is simple, as I predicted on Discord, we simply should use `get_all_type_vars()` instead of `get_type_vars()` (that specifically returns only `TypeVarType`). I also use this opportunity to tidy-up code in `bind_self()`, it should be now more readable, and much faster (especially when compiled with mypyc). cc @A5rocks --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@klen is the project you were running into this on open source? |
Bug Report
Mypy doesn't process ParamSpec correctly.
To Reproduce
Expected Behavior
Actual Behavior
Mypy output
Your Environment
mypy.ini
(and other config files):BTW: in pyright this code working fine
The text was updated successfully, but these errors were encountered: