Skip to content

Mypy requires argument after ** to be a dict when a Mapping is sufficient #3595

@rowillia

Description

@rowillia

Python doesn't need keyword arguments to be dictionaries, Mapping is sufficient.

Minimal Repro & contrived example:

from collections import Mapping


class M(Mapping):
    def keys(self):
        return ['a']

    def __getitem__(self, key):
        return 'world'

    def __len__(self):
        return 1

    def __iter__(self):
        return self.keys()


def foo(a: str=None) -> None:
    print(f'Hello {a}')


def bar(b: Mapping) -> None:
    foo(**b)


foo(**M())
bar(M())

Results in:

$ mypy test_mapping.py
test_mapping.py:23: error: Argument after ** must be a dictionary
test_mapping.py:26: error: Argument after ** must be a dictionary

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