Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pgjones opened this issue Oct 1, 2023 · 2 comments
Closed

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

pgjones opened this issue Oct 1, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@pgjones
Copy link

pgjones commented Oct 1, 2023

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.

@pgjones pgjones added the bug mypy got something wrong label Oct 1, 2023
@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 1, 2023

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.)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Oct 1, 2023
@pgjones
Copy link
Author

pgjones commented Oct 1, 2023

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
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants