Skip to content

Improve error message for incompatible **kwargs argument#21129

Open
yourlocaljosh wants to merge 2 commits intopython:masterfrom
yourlocaljosh:improve-kwargs-error-message
Open

Improve error message for incompatible **kwargs argument#21129
yourlocaljosh wants to merge 2 commits intopython:masterfrom
yourlocaljosh:improve-kwargs-error-message

Conversation

@yourlocaljosh
Copy link
Copy Markdown

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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

pyinstrument (https://github.com/joerick/pyinstrument)
+ pyinstrument/context_manager.py:40: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ pyinstrument/context_manager.py:63: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/deployments/runner.py:870: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ src/prefect/tasks.py:2255: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ src/prefect/cli/deploy/_schedules.py:70: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

colour (https://github.com/colour-science/colour)
+ colour/plotting/volume.py:785: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ colour/plotting/phenomena.py:1323: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ colour/plotting/phenomena.py:1370: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ colour/plotting/phenomena.py:1482: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ colour/plotting/phenomena.py:1538: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ colour/plotting/models.py:1984: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ colour/plotting/models.py:1988: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ colour/io/luts/tests/test_lut.py:521: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ colour/examples/contrast/examples_contrast.py:104: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+ src/hydra_zen/wrapper/_implementations.py:446: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

discord.py (https://github.com/Rapptz/discord.py)
+ discord/http.py:806: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ discord/client.py:727: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ discord/ext/commands/context.py:1141: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

ibis (https://github.com/ibis-project/ibis)
+ ibis/expr/types/temporal.py:1086: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

jax (https://github.com/google/jax)
+ jax/_src/xla_bridge.py:190: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ jax/_src/pallas/helpers.py:263: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ jax/_src/pallas/helpers.py:265: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ jax/_src/pallas/einshape.py:330: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ jax/_src/pallas/einshape.py:368: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

scipy (https://github.com/scipy/scipy)
+ scipy/fftpack/tests/gen_fftw_ref.py:43: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ scipy/fftpack/tests/gen_fftw_ref.py:59: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ scipy/fftpack/tests/gen_fftw_ref.py:74: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

apprise (https://github.com/caronc/apprise)
+ apprise/plugins/xmpp/base.py:422: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ apprise/plugins/xmpp/base.py:425: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

artigraph (https://github.com/artigraph/artigraph)
+ tests/arti/types/test_types.py:146: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict
+ tests/arti/types/test_types.py:150: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

pyppeteer (https://github.com/pyppeteer/pyppeteer)
+ pyppeteer/launcher.py:149: note: Consider annotating the ** argument as "**kwargs: Any" or using a TypedDict

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate better error message for incompatible **kwargs

1 participant