Skip to content

Mypy thinks dict[Never, Never] is a subtype of Mapping[str, object] #19641

Closed

Description

@randolf-scholz

This behaves as expected: https://mypy-play.net/?mypy=latest&python=3.12&gist=8718e96550599dc3ae48a13879005762

from typing import Mapping, Never

def upcast(d: dict[Never, Never]) -> Mapping[str, object]:
    return d  # E: [return-value] (✅ expected)

But it breaks here (due to caching?) https://mypy-play.net/?mypy=latest&python=3.12&gist=6199ba50b6b02e2350e70cb3a8ff4bba

from typing import Mapping, Never

def get[M: Mapping[str, object]](arg: M, /) -> M:
    return arg

reveal_type(get({}))  # dict[Never, Never] (❌ incorrect)

def upcast(d: dict[Never, Never]) -> Mapping[str, object]:
    return d  # <-- No error anymore 😱

Activity

  1. changed the title [-][caching?] Mypy thinks `dict[Never, Never]` is a subtype of `Mapping[str, object]`[/-] [+]Mypy thinks `dict[Never, Never]` is a subtype of `Mapping[str, object]`[/+] on Aug 11, 2025
  2. added a commit that references this issue on Aug 14, 2025
    74927d5
  3. randolf-scholz commented on Sep 19, 2025

    @randolf-scholz
    ContributorAuthor

    @sterliakov @ilevkivskyi This was only partially fixed; the caching part is adressed but mypy still incorrectly reveals dict[Never, Never] on master branch.

    https://mypy-play.net/?mypy=master&python=3.12&gist=79b0378f3b9997bc7bc1ffea94a7dc15

    from typing import Mapping, Never
    
    def get[M: Mapping[str, object]](arg: M, /) -> M:
        return arg
    
    reveal_type(get({}))  
    main.py:6: note: Revealed type is "builtins.dict[Never, Never]"
    

    But dict[Never, Never] is not a subtype of Mapping[str, object]...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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