-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
mypy fails to consider the case where *args or **kwds contains zero elements #4001
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
Comments
While your minimal repro indeed gives those errors (and probably shouldn't), in your actual example things are not so simple -- mypy only looks at your source code, it doesn't execute your code, so when it type-checks the line |
Is this related to #4335? |
I was confused because the original example in this issue used multiple inheritance as well. |
The first example |
Hi, I'd like to work on this as my first issue and I have a question. Lines 3790 to 3795 in 1677efd
However, an empty dictionary {} is currently typed as dict[<nothing>, <nothing>] and fails this check, presumably because dictionary types are contravariant at the key position. Would it be better to type {} as dict[<any>, <nothing>] ?
EDIT: I think #5580 is the same problem as this |
This issue is explained with |
I think it's difficult to judge whether a given dictionary is empty or not, so we cannot help but give up the "Too many arguments" check whenever there are arguments like
This is consistent with the case of lists and variable-length tuples:
At first I thought we can take advantage of the fact that an empty dictionary is typed as |
### 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.
Minimum repro:
This gives the following errors:
I think this is valid code and should pass the type check.
Context: I encountered this when I tried to write a multiple inheritance model where each class is passing arguments to the next parent (in terms of MRO):
You cannot expect a static derivation order due to the dynamic nature of MRO, so it's hard to avoid this error from happening.
The text was updated successfully, but these errors were encountered: