Skip to content

mypy matching **kwargs positionally instead of by name #7088

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
reggied opened this issue Jun 27, 2019 · 1 comment
Closed

mypy matching **kwargs positionally instead of by name #7088

reggied opened this issue Jun 27, 2019 · 1 comment

Comments

@reggied
Copy link

reggied commented Jun 27, 2019

I am reporting a bug illustrated by the following code.

from typing import Any


def caller() -> None:
    kwargs = dict(a=object(), b=object(), c=object())
    func(1, **kwargs)
    func(**kwargs)

def func(optional:int = 0, **kwargs:Any) -> None:
    pass

When I run version 0.670 of mypy with Python 3.7.1, I get:

a.py:7: error: Argument 1 to "func" has incompatible type "**Dict[str, object]"; expected "int"

It seems to be trying to match the **kwargs positionally instead of using the names. I would not expect mypy to produce an error on this code. You can see in line 6 that if I specify the optional argument explicitly, there is no error.

This same type of behavior was described in issue #1969, but I'm still getting the behavior with version 0.670, which includes those changes.

@ilevkivskyi
Copy link
Member

Mypy is technically right because it doesn't record which keys are present in a dictionary, it is just Dict[str, object]. One could potentially use TypedDict to avoid this, but then this is a duplicate of #5382.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants