Skip to content

Inconsistent behavior when matching dict against Mapping | Hashable #12467

Closed
@twoertwein

Description

@twoertwein

Bug Report

Mypy does not match dict[int, int] with Mapping[Hashable, Any] when it is in Union[Mapping[Hashable, Any], Hashable] but it matches when Mapping[Hashable, Any] is by itself.

To Reproduce

Example from @Dr-Irv:

from typing import Mapping, Hashable, Any, Union

def myfun(m: Mapping[Hashable, Any]):
    ...

def myfun2(m: Union[Mapping[Hashable, Any], Hashable]):
    ...

myfun({1: 1})  # passes
myfun2({1: 1})  # errors

Expected Behavior

No error.

Actual Behavior

error: Argument 1 to "myfun2" has incompatible type "Dict[int, int]"; expected "Union[Mapping[Hashable, Any], Hashable]"
note: Following member(s) of "Dict[int, int]" have conflicts:
note:     __hash__: expected "Callable[[], int]", got "None"

No error with pyright.

Your Environment

  • Mypy version used: 0.942

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