Skip to content

Don't map actual **kwargs to formal *args #6096

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

Merged
merged 2 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions mypy/argmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ def map_actuals_to_formals(actual_kinds: List[int],
no_certain_match = (
not formal_to_actual[fi]
or actual_kinds[formal_to_actual[fi][0]] == nodes.ARG_STAR)
if ((formal_names[fi] and no_certain_match)
or formal_kinds[fi] == nodes.ARG_STAR2):
if ((formal_names[fi]
and no_certain_match
and formal_kinds[fi] != nodes.ARG_STAR) or
formal_kinds[fi] == nodes.ARG_STAR2):
formal_to_actual[fi].append(ai)
return formal_to_actual

Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-kwargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,21 @@ class A:
tmp/m.py:1: error: Unsupported operand types for + ("int" and "str")
main:2: error: Unexpected keyword argument "x" for "A"
tmp/m.py:3: note: "A" defined here

[case testStarArgsAndKwArgsSpecialCase]
from typing import Dict, Mapping

def f(*vargs: int, **kwargs: object) -> None:
pass

def g(arg: int = 0, **kwargs: object) -> None:
pass

d = {} # type: Dict[str, object]
f(**d)
g(**d) # E: Argument 1 to "g" has incompatible type "**Dict[str, object]"; expected "int"

m = {} # type: Mapping[str, object]
f(**m)
g(**m) # TODO: Should be an error
[builtins fixtures/dict.pyi]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add the second test case mentioned in the issue? The one with:

def b(arg: str='', **kw: int) -> None:
    pass

Copy link
Member

@sobolevn sobolevn Nov 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three years later we have a similar error: #11583

But, calling b (in this case) with b(**{'x': 1}) will work in runtime. It will result in arg = '' (default value) and kw = {'x': 1}.
Not sure why this was explictly forbidden.

Do you remember why this was a problem?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My PR with the alternative solution: #11589