Skip to content
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

Give type annotation suggestion for incompatible **kwargs error #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,8 @@ def incompatible_argument(
quote_type_string(arg_type_str),
quote_type_string(expected_type_str),
)
if isinstance(arg_type, Instance):
notes = append_kwargs_notes(notes, arg_type, arg_kind)
expected_type = get_proper_type(expected_type)
if isinstance(expected_type, UnionType):
expected_types = list(expected_type.items)
Expand Down Expand Up @@ -3099,6 +3101,13 @@ def pretty_seq(args: Sequence[str], conjunction: str) -> str:
return ", ".join(quoted[:-1]) + last_sep + quoted[-1]


def append_kwargs_notes(notes: list[str], arg_type: Instance, arg_kind: ArgKind) -> list[str]:
"""Explain that annotating keyword arguments may resolve incompatible type issues."""
if arg_kind == ARG_STAR2 and arg_type.type.fullname == "builtins.dict":
notes.append('Consider using a TypedDict type or "Dict[str, any]" for the ** argument')
return notes


def append_invariance_notes(
notes: list[str], arg_type: Instance, expected_type: Instance
) -> list[str]:
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-columns.test
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def g(**x: int) -> None: pass
a = ['']
f(*a) # E:4: Argument 1 to "f" has incompatible type "*List[str]"; expected "int"
b = {'x': 'y'}
g(**b) # E:5: Argument 1 to "g" has incompatible type "**Dict[str, str]"; expected "int"
g(**b) # E:5: Argument 1 to "g" has incompatible type "**Dict[str, str]"; expected "int" \
# N:5: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
[builtins fixtures/dict.pyi]

[case testColumnsMultipleStatementsPerLine]
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,8 @@ kw2 = {'x': ''}
d2 = dict(it, **kw2)
d2() # E: "Dict[str, object]" not callable

d3 = dict(it, **kw2) # type: Dict[str, int] # E: Argument 2 to "dict" has incompatible type "**Dict[str, str]"; expected "int"
d3 = dict(it, **kw2) # type: Dict[str, int] # E: Argument 2 to "dict" has incompatible type "**Dict[str, str]"; expected "int" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
[builtins fixtures/dict.pyi]

[case testDictFromIterableAndStarStarArgs2]
Expand Down
45 changes: 39 additions & 6 deletions test-data/unit/check-kwargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ def f(a: int): pass
def g(): f(0, a=1)
[out]

[case testIncompatibleKeywordArgumentsClass]
from typing import Dict

class A:
def __init__(self, a: str, b: str, c: str, d: int = 2) -> None:
self.a: str = a
self.b: str = b
self.c: str = c
self.d: int = d

argument_a: str = "argument_a"
arguments_b_c: Dict[str, str] = {"b": "argument_b", "c": "argument_c"}
object_a: A = A(a=argument_a, **arguments_b_c) # E: Argument 2 to "A" has incompatible type "**Dict[str, str]"; expected "int" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
[builtins fixtures/dict.pyi]

[case testIncompatibleKeywordArgumentsJSON]
import json
json_kwargs = dict(indent=2)
json.dumps({}, **json_kwargs) # E: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "bool" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument \
# E: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Type[JSONEncoder]]" \
# E: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Tuple[str, str]]" \
# E: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Callable[[Any], Any]]"
[builtins fixtures/dict.pyi]

[case testInvalidKeywordArgument]
import typing
def f(a: 'A') -> None: pass # N: "f" defined here
Expand Down Expand Up @@ -301,9 +327,12 @@ d: Dict[str, A]
f(**d)
f(x=A(), **d)
d2: Dict[str, B]
f(**d2) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A"
f(x=A(), **d2) # E: Argument 2 to "f" has incompatible type "**Dict[str, B]"; expected "A"
f(**{'x': B()}) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A"
f(**d2) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
f(x=A(), **d2) # E: Argument 2 to "f" has incompatible type "**Dict[str, B]"; expected "A" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
f(**{'x': B()}) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
[builtins fixtures/dict.pyi]

[case testKwargsAllowedInDunderCall]
Expand Down Expand Up @@ -366,7 +395,8 @@ def f(a: 'A', b: 'B') -> None: pass
d: Dict[str, Any]
f(**d)
d2: Dict[str, A]
f(**d2) # E: Argument 1 to "f" has incompatible type "**Dict[str, A]"; expected "B"
f(**d2) # E: Argument 1 to "f" has incompatible type "**Dict[str, A]"; expected "B" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
class A: pass
class B: pass
[builtins fixtures/dict.pyi]
Expand Down Expand Up @@ -443,7 +473,8 @@ f(**a) # okay

b = {'': ''}
f(b) # E: Argument 1 to "f" has incompatible type "Dict[str, str]"; expected "int"
f(**b) # E: Argument 1 to "f" has incompatible type "**Dict[str, str]"; expected "int"
f(**b) # E: Argument 1 to "f" has incompatible type "**Dict[str, str]"; expected "int" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument

c = {0: 0}
f(**c) # E: Keywords must be strings
Expand Down Expand Up @@ -488,7 +519,8 @@ def g(arg: int = 0, **kwargs: object) -> None:

d = {} # type: Dict[str, object]
f(**d)
g(**d) # E: Argument 1 to "g" has incompatible type "**Dict[str, object]"; expected "int"
g(**d) # E: Argument 1 to "g" has incompatible type "**Dict[str, object]"; expected "int" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument

m = {} # type: Mapping[str, object]
f(**m)
Expand Down Expand Up @@ -563,4 +595,5 @@ main:37: error: Argument 1 to "foo" has incompatible type "**B[str, str]"; expec
main:38: error: Argument after ** must be a mapping, not "C[str, float]"
main:39: error: Argument after ** must be a mapping, not "D"
main:41: error: Argument 1 to "foo" has incompatible type "**Dict[str, str]"; expected "float"
main:41: note: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
[builtins fixtures/dict.pyi]
6 changes: 4 additions & 2 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,8 @@ def c3(f: Callable[P, int], *args, **kwargs) -> int: ...
def c4(f: Callable[P, int], *args: int, **kwargs: str) -> int:
# but not ok to call:
f(*args, **kwargs) # E: Argument 1 has incompatible type "*Tuple[int, ...]"; expected "P.args" \
# E: Argument 2 has incompatible type "**Dict[str, str]"; expected "P.kwargs"
# E: Argument 2 has incompatible type "**Dict[str, str]"; expected "P.kwargs" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
return 1

def f1(f: Callable[P, int], *args, **kwargs: P.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
Expand Down Expand Up @@ -1287,7 +1288,8 @@ def c3(f: Callable[Concatenate[int, P], int], *args, **kwargs) -> int: ...
def c4(f: Callable[Concatenate[int, P], int], *args: int, **kwargs: str) -> int:
# but not ok to call:
f(1, *args, **kwargs) # E: Argument 2 has incompatible type "*Tuple[int, ...]"; expected "P.args" \
# E: Argument 3 has incompatible type "**Dict[str, str]"; expected "P.kwargs"
# E: Argument 3 has incompatible type "**Dict[str, str]"; expected "P.kwargs" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
return 1

def f1(f: Callable[Concatenate[int, P], int], *args, **kwargs: P.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,8 @@ Weird = TypedDict("Weird", {"@": int})
def foo(**kwargs: Unpack[Weird]) -> None:
reveal_type(kwargs["@"]) # N: Revealed type is "builtins.int"
foo(**{"@": 42})
foo(**{"no": "way"}) # E: Argument 1 to "foo" has incompatible type "**Dict[str, str]"; expected "int"
foo(**{"no": "way"}) # E: Argument 1 to "foo" has incompatible type "**Dict[str, str]"; expected "int" \
# N: Consider using a TypedDict type or "Dict[str, any]" for the ** argument
[builtins fixtures/dict.pyi]

[case testUnpackKwargsEmpty]
Expand Down
Loading