-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
mypy 1.6.1, ParamSpec: Cannot infer type argument 1 of "function_name"
#16301
Comments
This is probably the same thing as #16296, though I haven't looked too deeply |
It might, I'm not 100% sure. Their example uses |
The function def case(*args: P.args, **kwargs: P.kwargs) -> Case[P]:
return Case() Mypy should generate an error here. Pyre was the reference implementation for PEP 612, and it emits an error here. Pyright does as well. Perhaps this bug should be changed to cover this false negative. |
Thanks @erictraut, I missed that (but in my defence, I only skimmed it earlier ;) In that case, yes -- there's a mypy bug here, but the bug is that it's not rejecting the |
The error itself is a duplicate of #16296. About the PEP 612: on one hand I understand the motivation, there is no meaningful mapping from actual kinds to formal kinds, but on the other hand this looks somewhat arbitrary, since e.g. a similar case |
So, what's the conclusion here? #16296 is going to be eventually fixed and with that my code example will start working again as well? Or it won't be fixed because the PEP says it shouldn't be supported and so I should find a different API for my package? Another thing to note is that while the |
I changed the code to get rid of the constructor and not use the case forbidden by PEP, but the issue persists. The updated code example: from typing import Callable, Generic, ParamSpec
P = ParamSpec('P')
class Case(Generic[P]):
def __init__(self, *args: P.args, **kwargs: P.kwargs):
pass
def _test(a: int):
assert a
def parametrize(func: Callable[P, None], case: Case[P]):
pass
parametrize(_test, Case(1)) |
@orsinium Thanks for this example! It shows why the restriction in the PEP is arbitrary. PEP talks something about ParamSpec being "in scope", here it is clearly in scope. The real problem is, as I said, that actual kinds don't map meaningfully to formal kinds. So the conclusion here is that your code will start working again when the other issue is fixed. Mypy will continue to support this (arguably tricky) use case on best effort basis. It may prohibit it at some point, if people will strongly prefer to do so. |
with 1.7 it works for the valid case but fails with invalid parametrize(_test, case(1)) # OK
parametrize(_test, case("1")) # Cannot infer type argument 1 of "parametrize" [misc] |
It still fails for me for more involved cases, even for valid ones. The original example I posted passes for valid case, though. I'll try to make another example that reproduces the current failure and open a new issue. |
Created a new issue: #16485 Now it fails if |
In pytypest, inference for code using ParamSpec started to fail in mypy 1.6.1.
To Reproduce
Expected Behavior
On mypy==1.5.1, no issues are reported.
Actual Behavior
On mypy==1.6.1, inference fails:
Your Environment
mypy.ini
(and other config files): noneI also tried using
--new-type-inference
, it doesn't help.The text was updated successfully, but these errors were encountered: