Improve error message for incompatible **kwargs argument by yourlocaljosh · Pull Request #21129 · python/mypy

Fixes #8874

When a function is called with **kwargs whose value types are
incompatible with the expected parameter types, mypy produced
multiple confusing errors with no actionable guidance. This PR
adds a note suggesting the user annotate the ** argument as
**kwargs: Any or use a TypedDict for more precise typing.

Before:
test.py:4: error: Argument 1 to "f" has incompatible type "**dict[str, int]"; expected "str"

After:
test.py:4: error: Argument 1 to "f" has incompatible type "**dict[str, int]"; expected "str"
test.py:4: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

Notes:

  • Addresses the feedback from Give type annotation suggestion for incompatible **kwargs error #17186, which suggested using
    **kwargs: Any annotation syntax rather than Dict[str, Any]
    since that's what a user would actually write in their function
    signature.
  • Tests added in check-kwargs.test. I updated existing tests across 8
    test files to reflect the new note.