-
-
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
mypy shows type error with json.dump(s) and kwargs #8772
Comments
Same happens when passing from typing import Dict
class A:
"""Class A."""
def __init__(self, a: str, b: str, c: str, d: int = 2) -> None:
"""Construct A object."""
self.a: str = a
self.b: str = b
self.c: str = c
self.d: int = d
def main() -> None:
"""Run main method."""
argument_a: str = "argument_a"
arguments_b_c: Dict[str, str] = {"b": "argument_b", "c": "argument_c"}
object_a: A = A(a=argument_a, **arguments_b_c)
print(object_a)
if __name__ == "__main__":
main() Gives the error:
Edit: Added error. |
I feel like there's two issues here (neither of which is directly related to
|
To elaborate on the last point (and to check that I understand the semantics here), I believe that mypy is enforcing that the type of the value of the expanded dict must match every possible keyword argument to the function called. Therefore, if the function's keyword arguments take incompatible types, only a dict whose values are typed as I was instead expecting mypy to err on the side of generosity and instead assume that if the value type of the dict matches the type of any of the keyword arguments of the function, the caller probably knows what they're doing. I'm now not sure which approach is better. The approach I was expecting is less safe, but mypy's current approach is going to result in a lot of false positives. I suppose the workaround of typing the dict with |
Yeah, my inclination here is to say not-a-bug. Though if somebody wants to open a bug to argue for looser interpretation of kwargs typing, that would be reasonable. |
Would it be possible to mention this directly in the documentation under common issues? I've seen multiple people get confused by this behavior, and it can be quite difficult to understand what's happening because of the way the type mismatch is reported. |
Added #8874 about improving the generated error messages. |
I would argue to lean towards less safe, but assuming the caller probably knows what they're doing. Ask forgiveness, not permission. |
I think aspects of this behaviour have been changed on master, so if you have complaints, please make sure they're up to date. |
mypy doesn't like kwargs python/mypy#8772
I have noticed that in the seemingly common situation of passing
**kwargs
tojson.dump
orjson.dumps
,mypy
fails the type checks unless the kwargs are explicitly annotated asDict[str, Any]
. A simple example:Which results in:
If I change the example to this:
The errors are the same, except the inferred type changes to
**Dict[str, object]
. I can work around it like so:Using Python 3.8 and
mypy == 0.770
on Arch Linux. The error is still present when using the current master (commit 2a3de7b).I have also tested this with
pytype
andpytype
does not have the same failure.The text was updated successfully, but these errors were encountered: