Skip to content

Incorrect error message Dict[str, str] not allowed to Dict[Hashable, Any] #12464

Closed
@Dr-Irv

Description

@Dr-Irv

Bug Report

If mypy infers a variable to be Dict[str, str], then it does not allow it to be passed to a function with the type of Mapping[Hashable, Any] or Union[Mapping[Hashable, Any], Hashable]

mypy does not allow {1, 1} to be passed to an argument of type Union[Mapping[Hashable, Any], Hashable], but is OK with a type of Mapping[Hashable, Any]

To Reproduce

(Write your steps here:)

from typing import Mapping, Hashable, Any, Union, Dict


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


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


def myfun3(m: Union[Dict[Hashable, Any], Hashable]):
    pass


def myfun4(m: Dict[Hashable, Any]):
    pass


myfun({1: 2})

myfun2({1: 2})  # incorrect error reported

myfun3({1: 2})

col_map = {"a": "b"}
myfun(col_map)  # incorrect error reported
myfun2(col_map)  # incorrect error reported
myfun3(col_map)  # incorrect error reported
myfun4(col_map)  # incorrect error reported

myfun({"a": "b"})  # no error reported
myfun2({"a": "b"})  # incorrect error reported
myfun3({"a": "b"})  # no error reported
myfun4({"a": "b"})  # no error reported

Expected Behavior
No errors reported

Actual Behavior

mapintint.py:22: error: Argument 1 to "myfun2" has incompatible type "Dict[int, int]"; expected "Union[Mapping[Hashable, Any], Hashable]"
mapintint.py:27: error: Argument 1 to "myfun" has incompatible type "Dict[str, str]"; expected "Mapping[Hashable, Any]"
mapintint.py:28: error: Argument 1 to "myfun2" has incompatible type "Dict[str, str]"; expected "Union[Mapping[Hashable, Any], Hashable]"
mapintint.py:29: error: Argument 1 to "myfun3" has incompatible type "Dict[str, str]"; expected "Union[Dict[Hashable, Any], Hashable]"
mapintint.py:30: error: Argument 1 to "myfun4" has incompatible type "Dict[str, str]"; expected "Dict[Hashable, Any]"
mapintint.py:33: error: Argument 1 to "myfun2" has incompatible type "Dict[str, str]"; expected "Union[Mapping[Hashable, Any], Hashable]"

Your Environment

  • Mypy version used: 0.942
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.7
  • Operating system and version: Windows 10

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