Skip to content

Errors when calling a function that has **kwargs and default value parameter (non-trivial type) #8862

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

Open
MatejKastak opened this issue May 20, 2020 · 0 comments
Labels
topic-calls Function calls, *args, **kwargs, defaults

Comments

@MatejKastak
Copy link

Hello, when I declare a function that has **kwargs along with another parameter with a default value, mypy reports errors when trying to call this function without the default argument.

Repro

def foo(option: bool = None, **kwargs):
    pass

def bar(option: str = '', **kwargs):
    pass


d = {'test': True}

foo(**{'test': True})
foo(**d)
foo(other_option=True, option=True, **d)
foo(option=True, **{'test': True})

bar(**{'test': True})  # Error
bar(**d)  # Error
bar(other_option=True, **d)  # Error
bar(option='Hello, world!', **{'test': True})

Mypy output:

test.py:15: error: Argument 1 to "bar" has incompatible type "**Dict[str, bool]"; expected "str"
test.py:16: error: Argument 1 to "bar" has incompatible type "**Dict[str, bool]"; expected "str"
test.py:17: error: Argument 2 to "bar" has incompatible type "**Dict[str, bool]"; expected "str"
Found 3 errors in 1 file (checked 1 source file)

Other failing inputs

From my observations: default parameters with {bool, int, float} types work fine (like the foo function). But if the default parameter has type of any of the following types:

  • Custom classes
  • Enums
  • bytes
  • str

mypy will report errors.

Expected result

foo and bar should behave the same way (e.g. no errors).

Versions

mypy 0.770 and Python 3.8.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-calls Function calls, *args, **kwargs, defaults
Projects
None yet
Development

No branches or pull requests

2 participants