python / python/mypy

Errors for incorrect type on `**kwargs` should give the name of the argument

Open
#16,490 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-error-reporting topic-overloads topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

In case of **kwargs being passed to an overloaded function, one issue gets raised for every overload of that function. This problem is not present when passing a regular incompatible type to that overloaded function, eg. int

To Reproduce

import json
from typing import Any

def load(data: str, **kwargs: object) -> Any:
    return json.loads(data, **kwargs)

Gist: https://mypy-play.net/?mypy=master&python=3.11&gist=8d7f8bb50c5f9022469b59ec127df614

The bug won't reproduce for eg. if **kwargs: object was replaced with x: object.

Expected Behavior

Single issue being raised.

Actual Behavior

$ mypy asd.py
asd.py:5: error: Argument 2 to "loads" has incompatible type "**dict[str, object]"; expected "type[JSONDecoder] | None"  [arg-type]
asd.py:5: error: Argument 2 to "loads" has incompatible type "**dict[str, object]"; expected "Callable[[dict[Any, Any]], Any] | None"  [arg-type]
asd.py:5: error: Argument 2 to "loads" has incompatible type "**dict[str, object]"; expected "Callable[[str], Any] | None"  [arg-type]
asd.py:5: error: Argument 2 to "loads" has incompatible type "**dict[str, object]"; expected "Callable[[list[tuple[Any, Any]]], Any] | None"  [arg-type]
Found 4 errors in 1 file (checked 1 source file)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided mypy-play reproducer or the shown mypy asd.py command, then trace how overloaded calls handle **kwargs diagnostics. Done means the example reports one incompatible-argument error rather than one error per overload, with a regression test covering the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.