-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix argument checking on empty dict with double stars #9629
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a5dcac8
Infer type of **kwargs with a context of Mapping[str, Any]
momohatt 2a88bb8
Relax 'Too many arguments' check for **kwargs
momohatt 13dc4d2
Rename test
momohatt ff90cf3
Relax check on kwarg key type
momohatt 989512b
Add a test to prevent inferring Dict[str, Any]
momohatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,8 +299,9 @@ d = None # type: Dict[str, A] | |
f(**d) | ||
f(x=A(), **d) | ||
d2 = None # type: Dict[str, B] | ||
f(**d2) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A" | ||
f(x=A(), **d2) # E: Argument 2 to "f" has incompatible type "**Dict[str, B]"; expected "A" | ||
f(**d2) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A" | ||
f(x=A(), **d2) # E: Argument 2 to "f" has incompatible type "**Dict[str, B]"; expected "A" | ||
f(**{'x': B()}) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A" | ||
class A: pass | ||
class B: pass | ||
[builtins fixtures/dict.pyi] | ||
|
@@ -396,7 +397,7 @@ class A: pass | |
from typing import Any, Dict | ||
def f(*args: 'A') -> None: pass | ||
d = None # type: Dict[Any, Any] | ||
f(**d) # E: Too many arguments for "f" | ||
f(**d) | ||
class A: pass | ||
[builtins fixtures/dict.pyi] | ||
|
||
|
@@ -491,3 +492,11 @@ m = {} # type: Mapping[str, object] | |
f(**m) | ||
g(**m) # TODO: Should be an error | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testPassingEmptyDictWithStars] | ||
def f(): pass | ||
def g(x=1): pass | ||
|
||
f(**{}) | ||
g(**{}) | ||
[builtins fixtures/dict.pyi] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a test case where we check the this continues to generate an error:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this no longer an error?
Uh oh!
There was an error while loading. Please reload this page.
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.
I think this is addressed by the author (not me) in the PR description.
The rationale being that since a
Dict[Any, Any]
could be an empty dictionary, so it might not be the case that there are too many arguments. I think this is a similar rationale to how indexing adict
where the key may not exist is not a type error, but instead deferred to runtime.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.
I'm open to persuasion on this, but to me it seems totally reasonable to give an error for
f(**kwargs)
whenf
takes no keyword arguments at all. It's true thatkwargs
could be empty, but the code makes little sense anyway.Uh oh!
There was an error while loading. Please reload this page.
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.
@JelleZijlstra One problem with giving an error on that is that it's a common pattern in Python's cooperative multiple inheritance to do forwarding like so:
SomeMixin
has no idea what class super is, so it should forward keyword arguments at least. Then, it can be used like so:Not everyone uses cooperative multiple inheritance, but I don't think this pattern should raise any type errors.
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.
Thanks, that's a good point! Let's just land this in that case.
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.
Thank you so much. I've been looking forward to this 😄