-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Improve ambiguous **kwarg checking #9573
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
Conversation
mypy/checkexpr.py
Outdated
# **kwargs which cannot be mapped with certainty (non-TypedDict | ||
# **kwargs). | ||
and not all(actual_kinds[m] == nodes.ARG_STAR2 and | ||
not isinstance(actual_types[m], TypedDictType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mypy/checkexpr.py:4131: error: Never apply isinstance() to unexpanded types;
I don't see a terribly clean way to fix this. # type: ignore
okay here? Or other suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not isinstance(actual_types[m], TypedDictType) | |
not isinstance(get_proper_type(actual_types[m]), TypedDictType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks. I totally misunderstood the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks!
### Description Closes #5580 Previously, the type of empty dicts are inferred as `dict[<nothing>, <nothing>]`, which is not a subtype of `Mapping[str, Any]`, and this has caused a false-positive error saying `Keywords must be strings`. This PR fixes it by inferring the types of double-starred arguments with a context of `Mapping[str, Any]`. Closes #4001 and closes #9007 (duplicate) Do not check for "too many arguments" error when there are any double-starred arguments. This will lead to some false-negavites, see my comment here: #4001 (comment) ### Test Plan Added a simple test `testPassingEmptyDictWithStars`. This single test can cover both of the issues above. I also modified some existing tests that were added in #9573 and #6213.
Description
Fixes #4708
Allows for multiple ambiguous **kwarg unpacking in a call -- all ambiguous **kwargs will map to all formal args that do not have a certain actual arg.
Fixes #9395
Defers ambiguous **kwarg mapping until all other unambiguous formal args have been mapped -- order of **kwarg unpacking no longer affects the arg map.
Test Plan
Additional tests included in PR, no existing tests were changed. Below are new tests that would exhibit different behaviors if they're run without code changes in the PR:
f1(**d, **d2)
produces no error in PR. Current producesToo many arguments for "f1"
(#4708)f2(**d, **d2)
produces no error in PR. Current producesToo many arguments for "f2"
(#4708)f1(**d, **td)
produces no error in PR. Current produces"f1" gets multiple values for keyword argument "x"
anderror: "f1" gets multiple values for keyword argument "y"
(#9395)f2(**d, **td)
producesToo many arguments for "f2"
. Current produces"f2" gets multiple values for keyword argument "x"
and"f2" gets multiple values for keyword argument "y"
(#9395)