Skip to content

Mapping invariant on key? #16103

@gsakkis

Description

@gsakkis

It seems that Mapping[k, v] is invariant to k, but not when a dict literal is passed.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=4195e62f5ea8fa4b027988aa9174bb7c

main.py:25: error: Argument 1 to "test_map_key" has incompatible type "Mapping[str, int]"; expected "Mapping[str | int, Any]"  [arg-type]
  1. Why is this an error? Mapping is immutable so it shouldn't matter that the passed key is more constrained that what is expected.
  2. Why does passing a dict literal work?

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