Skip to content

Spurious "Too many arguments" when unpacking empty dicts #9007

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
chkno opened this issue Jun 16, 2020 · 2 comments · Fixed by #9629
Closed

Spurious "Too many arguments" when unpacking empty dicts #9007

chkno opened this issue Jun 16, 2020 · 2 comments · Fixed by #9629
Labels
false-positive mypy gave an error on correct code priority-1-normal

Comments

@chkno
Copy link

chkno commented Jun 16, 2020

mypy gives incorrect Too many arguments errors when unpacking empty dicts to zero-argument functions. Unpacking empty lists works fine.

from typing import Dict, List, NamedTuple

def f1():
    pass

class C1(NamedTuple):
    pass

d_zero: Dict[str,str] = {}

print((lambda: 42)(**{}))     # error: Too many arguments
print((lambda: 42)(**d_zero)) # error: Too many arguments

print(f1(**{}))     # error: Too many arguments for "f1"
print(f1(**d_zero)) # error: Too many arguments for "f1"

print(C1(**{}))     # error: Too many arguments for "C1"
print(C1(**d_zero)) # error: Too many arguments for "C1"

# Empty lists work fine in recent versions:
l_zero: List[str] = []
print((lambda: 42)(*[]))
print((lambda: 42)(*l_zero))
print(f1(*[]))
print(f1(*l_zero))
print(C1(*[]))
print(C1(*l_zero))

I observe this problem all three versions of mypy that I tried: 0.701 0.761 0.770. The list examples also incorrectly give errors in 0.701, but this has been fixed in 0.761 and 0.770.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 19, 2020

Yeah, it seems that mypy should assume that the dictionary can be empty, for consistency with the list case.

@NeilGirdhar
Copy link
Contributor

This looks like #4001

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
false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants