Skip to content

Generate better error message for incompatible **kwargs #8874

Open
@JukkaL

Description

@JukkaL

This output is from #8772:

$ mypy test.py 
test.py:6: error: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "bool"
test.py:6: error: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Type[JSONEncoder]]"
test.py:6: error: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Tuple[str, str]]"
test.py:6: error: Argument 2 to "dumps" has incompatible type "**Dict[str, int]"; expected "Optional[Callable[[Any], Any]]"
Found 4 errors in 1 file (checked 1 source file)

The error messages are pretty confusing. We should special case this and produce a customized error message that explains the situation better.

Here's the code that triggers the errors:

import json

json_kwargs = dict(indent=2)

json.dumps({}, **json_kwargs)

Some ideas about what to do:

  • Generate a single error message instead of multiple ones.
  • Add a note suggesting the use of Dict[str, Any] or a TypedDict type for the **kwargs argument.
  • Maybe also explain that mypy matches the value type against all arguments, or add a link to documentation (add something to common issues, for example).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions