Skip to content

dict considered incompatible with dict type with supertype for key #15030

Description

@jwodder

Bug Report

Consider the following code:

from pathlib import Path

def mkfiles(spec: dict[str | Path, str]) -> None:
    ...

spec: dict[str, str] = {
    "foo.txt": "Fooey",
    "bar.txt": "Bar-y",
}

mkfiles(spec)

Running mypy on this code with the default options produces the following error:

superkey01.py:11: error: Argument 1 to "mkfiles" has incompatible type "Dict[str, str]"; expected "Dict[Union[str, Path], str]"  [arg-type]

Changing dict to collections.abc.Mapping does not get rid of the error.

Expected Behavior

mypy should have accepted the code, as the key type str | Path is more general than str.

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.3 on macOS 11.7.2

Activity

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