-
Notifications
You must be signed in to change notification settings - Fork 440
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
False positive when a function accepts **kwargs after arguments with default values #954
Comments
Thanks @MaigoAkisame for the detailed report. Function argument matching is pretty tricky to get right, and in general has false negative / false positive tradeoffs (in this particular example the dict happens to a literal so we could in principle synthesize an exact typed dict match, but currently Pyre doesn't automatically syntheseize typed dicts and I think neither does mypy) We may be able to do better though, this is a good example. And you're certainly write that the repeated use of "1st" in error messages is strange and a bit confusing |
Inferring the type of the values in In my example, the important thing is to figure out How difficult do you estimate the fix will be? |
I think this is a duplicate of #242, or at least the solution is the same. Either you can declare the dictionary as a typed dict:
or you can unpack the dictionary literal directly in the call
Both of those would work on the latest version of Pyre. When you unpack a dictionary literal directly in the call, we have a special case that expands it to named arguments. Outside of that, we don't model heterogeneous dictionaries that aren't a typed dict. |
@yangdanny97 For more context, I'm writing a function like Currently my solution is to write the
Admittedly, this makes the signature more obscure, and loses the type checking on And BTW, the issue I'd like to have resolved is not having pyre infer the types of the values in Will this be easier to fix? |
So technically it's not a "bug" in the sense that it doesn't violate the typing specification for Python; the spec mentions that the behavior for checking non-typed dictionaries varies by typechecker, and the behavior of Pyre v.s. Mypy v.s. Pyright are all different here. What you're asking for sounds like structural typing for non-typed dictionaries, which would be a nontrivial feature to implement since we'd need to track the set of known keys through |
I'm not asking to infer the types of the elements in Are you saying even inferring which keys exist in |
Yes, because there's no mechanism in Pyre to track known keys in a non-typed dictionary, and the behavior isn't standardized so even if we implemented something the other typecheckers would give different results depending on their implementation details. The easiest solution would be to ignore operations like del, assignment, spread, and just drop all the known keys and fall back to |
Thanks for the explanation! I think I'll use the |
Pyre Bug
Bug description
Pyre mistakenly reports an "incompatible parameter type [6]" error when a function's signature declares
**kwargs
after arguments with default values, and a caller tries to provide values forkwargs
with a**
expansion.Reproduction steps
Here's a minimal example:
Run pyre, and you'll get the following errors:
Apparently, pyre is matching the values in
dic
with the argumentsa
,b
, andc
.Also, the word
1st
in the error messages isn't always accurate:b
andc
are the 2nd and 3rd positional arguments.Expected behavior
There should be no error, because the contents of
dic
should be matched with**kwargs
.Logs
N/A
Additional context
Maybe a duplicate of #242, but it's rather hard to read, and I'm not sure if I understand it.
Another Python type checker,
mypy
, has this bug, too. It has been reported repeatedly in the last 8 years (examples below), but hasn't been fixed yet.python/mypy#8485 (reported by the same user as the issue above)
python/mypy#1969 (first report in 2016)
python/mypy#5382 (contains links to all similar reports)
python/mypy#8862
The text was updated successfully, but these errors were encountered: