Skip to content

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

@randolf-scholz

Description

@randolf-scholz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions