Skip to content

Optional arguments mixed with **kwargs #11084

@bluefish6

Description

@bluefish6

Bug Report

Mypy seems to try to compare provided arguments from **kwargs dict against specified arguments with default values.

While the code executes and runs perfectly fine in cpython 3.8.10, mypy says it's incorrect.

I think it might be related to #9676 as the initial symptom seems to match what I am observing in my case. However, I think that examples discussed there are different from the ones I encounter (as in: "possibly of different origin").

To Reproduce

somefile.py

def my_function(
    foo: bool = False,
    baz: bool = False,
    **additional,
):
    print(foo)
    print(baz)
    print(additional)


additional_kwargs = {"somearg1": 1}
my_function(**additional_kwargs)                      # line 12
my_function(foo=True, **additional_kwargs)            # line 13
my_function(baz=True, **additional_kwargs)            # line 14
my_function(foo=True, baz=True, **additional_kwargs)  # line 15

 python somefile.py 

False
False
{'somearg1': 1}

True
False
{'somearg1': 1}

False
True
{'somearg1': 1}

True
True
{'somearg1': 1}

somefile2.py

def my_function2(
    foo: bool = False,
    baz: int = 666,
    **additional,
):
    print(foo)
    print(baz)
    print(additional)


additional_kwargs = {"somearg1": 1}
my_function2(**additional_kwargs)                      # line 12
my_function2(foo=True, **additional_kwargs)            # line 13
my_function2(baz=234, **additional_kwargs)             # line 14
my_function2(foo=True, baz=234, **additional_kwargs)   # line 15
python somefile2.py 

False
666
{'somearg1': 1}

True
666
{'somearg1': 1}

False
234
{'somearg1': 1}

True
234
{'somearg1': 1}

Expected Behavior

No errors

Actual Behavior

$ mypy somefile.py 
somefile.py:12: error: Argument 1 to "my_function" has incompatible type "**Dict[str, int]"; expected "bool"
somefile.py:13: error: Argument 2 to "my_function" has incompatible type "**Dict[str, int]"; expected "bool"
somefile.py:14: error: Argument 2 to "my_function" has incompatible type "**Dict[str, int]"; expected "bool"
Found 3 errors in 1 file (checked 1 source file)

For the second file, note that no error was reported on line 13 below. My guess is that the somearg1 (int) was (incorrectly!) matched to baz (int):

mypy somefile2.py 
somefile2.py:12: error: Argument 1 to "my_function2" has incompatible type "**Dict[str, int]"; expected "bool"
somefile2.py:14: error: Argument 2 to "my_function2" has incompatible type "**Dict[str, int]"; expected "bool"
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 0.910 (same results also on mypy 0.812 and even on 0.620)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: cpython 3.8.10
  • Operating system and version: Ubuntu 20.04.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions