Skip to content

mypy confusion with empty dict as a Mapping when within a union #16208

Closed as not planned

Description

@pgjones

I've created a file.py with the following content,

from typing import Any, Mapping

a: tuple[Mapping[str, Any], dict] | tuple[Mapping[str, Any], int] = {}, 200
b: tuple[Mapping[str, Any], int] = {}, 200 

Then installed mypy (Python 3.11.1),

$ pip install mypy
Collecting mypy
  Using cached mypy-1.5.1-cp311-cp311-macosx_10_9_x86_64.whl (10.5 MB)
Collecting typing-extensions>=4.1.0
  Using cached typing_extensions-4.8.0-py3-none-any.whl (31 kB)
Collecting mypy-extensions>=1.0.0
  Using cached mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)
Installing collected packages: typing-extensions, mypy-extensions, mypy
Successfully installed mypy-1.5.1 mypy-extensions-1.0.0 typing-extensions-4.8.0

Which gives this error,

mypy file.py
file.py:3: error: Incompatible types in assignment (expression has type "tuple[dict[<nothing>, <nothing>], int]", variable has type "tuple[Mapping[str, Any], dict[Any, Any]] | tuple[Mapping[str, Any], int]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Which is odd as line 4 (the b assignment) is fine.

Note this relates to a typing issue in Flask.

Activity

  1. AlexWaygood commented on Oct 1, 2023

    @AlexWaygood
    Member

    This bug is reproducible with mypy 1.5.1, but has already been fixed on the master branch: https://mypy-play.net/?mypy=master&python=3.11&gist=2f5367d2a6b23d57ad81f2aabbcbe5ea. It was likely fixed by #16122 (but I haven't checked that).

    (The fix probably won't be included in mypy 1.6, unfortunately; you'll probably have to wait for mypy 1.7.)

  2. pgjones commented on Oct 1, 2023

    @pgjones
    Author

    Thanks, great to hear it has been fixed.

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