Skip to content

incompatible type when combining a literal with keyword expansion #12977

@kretes

Description

@kretes

Bug Report
Having a code snippet:

dict1 = {"a": 1, "b": "a"}
dict2 = {"c": 1, "d": 2, **dict1}

I receive
error: Argument 1 to "update" of "MutableMapping" has incompatible type "Dict[str, object]"; expected "SupportsKeysAndGetItem[str, int]" from mypy

To Reproduce
run mypy on the snippet

Expected Behavior
dict1 is Dict[str, object] and hence dict2 should be and mypy shouldn't raise that.

Actual Behavior
I think this gets analyzed sequentially and so first the dict2 type is inferred from the literal entries and gets set to Dict[str, int] which then doesn't accept to call an update using entries from another dict of broader type.

Considering than in Python's syntax this is a single expression - this shouldn't happen like this.

Adding an explicit type to dict2 is a workaround

Your Environment

  • Mypy version used: 0.960
  • Python version used: 3.9.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions