python / python/mypy

False positive error on `**kwargs` with `Mapping[Literal, Any]` type

Open
#20,424 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

https://mypy-play.net/?mypy=master&python=3.12&flags=verbose&gist=c3db401cf676c8c593dec1c525f7e748

Mypy Version: 1.20.0+dev.d06d3d9cfd6611c0e64c0df59fc0449754e47ed8

Fixed by: #20419

See Also: #20416

from typing import Literal, Mapping, Iterable
def func(a: int, b: int) -> None: ...

class GOOD_KW:
    def keys(self) -> Iterable[Literal["a", "b"]]: ...
    def __getitem__(self, key: str) -> int: ...

class BAD_KW:
    def keys(self) -> Iterable[Literal["one", 1]]: ...
    def __getitem__(self, key: str) -> int: ...

def test(
    good_kw: GOOD_KW,
    bad_kw: BAD_KW,
    good_dict: dict[Literal["a", "b"], int],
    bad_dict: dict[Literal["one", 1], int],
    good_mapping: Mapping[Literal["a", "b"], int],
    bad_mapping: Mapping[Literal["one", 1], int],
) -> None:
    func(**good_kw)
    func(**bad_kw)  # E: Argument after ** must have string keys
    func(**good_dict)
    func(**bad_dict)  # E: Argument after ** must have string keys
    func(**good_mapping)  # ❌️ False positive
    func(**bad_mapping)  # E: Argument after ** must have string keys

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 with the linked mypy-play reproducer and compare the GOOD_KW, BAD_KW, dict, and Mapping cases. Read #20419, which the issue identifies as the fix, and #20416 for related context. Done means the Mapping case no longer reports a false positive while the invalid non-string key cases still produce errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.