Skip to content

Weird, but legal ** syntax not accepted #5580

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

Closed
warsaw opened this issue Sep 6, 2018 · 1 comment · Fixed by #9629
Closed

Weird, but legal ** syntax not accepted #5580

warsaw opened this issue Sep 6, 2018 · 1 comment · Fixed by #9629
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low

Comments

@warsaw
Copy link
Member

warsaw commented Sep 6, 2018

I saw this in real code, although I don't really understand the reason why this is done. Still, even though it's a bit weird, it's legal and I think mypy should allow it:

class Foo:
    def __init__(self, a=1, b=2):
        self.a = a
        self.b = b


Foo(**{})
$ mypy foo.py
foo.py:7: error: Keywords must be strings
@gvanrossum
Copy link
Member

Agreed -- the dict could be type-checked here with a context of Mapping[str, Any] and that would make the empty dict have the type Dict[str, Any] rather than Dict[<nothing>, <nothing>].

Low priority though.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-2-low false-positive mypy gave an error on correct code labels Sep 6, 2018
JelleZijlstra pushed a commit that referenced this issue Aug 26, 2021
### 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants