Skip to content
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

mypy doesn't recognize incompatible type in Dict #2538

Open
etibger opened this issue Dec 8, 2016 · 3 comments
Open

mypy doesn't recognize incompatible type in Dict #2538

etibger opened this issue Dec 8, 2016 · 3 comments
Labels
bug mypy got something wrong priority-1-normal

Comments

@etibger
Copy link

etibger commented Dec 8, 2016

On windows,

python --version
Python 3.5.2

with the following mypy_bug.py:
from typing import Dict

CHESSDICT = {} # type: Dict[str, int]
CHESSDICT.update({("a", 1): "Rook"})
CHESSDICT.update({(0, 0): "Rook"})
for key in CHESSDICT:
print('position {0}, figure {1}'.format(key, CHESSDICT[key]))

python -m mypy -v .\mypy_bug.py
LOG: Mypy version 0.4.6
LOG: Parsing .\mypy_bug.py (mypy_bug)
LOG: Parsing C:\Users\user\venv\lib\mypy\typeshed\stdlib\3\typing.pyi (typing)
LOG: Parsing C:\Users\user\venv\lib\mypy\typeshed\stdlib\3\builtins.pyi (builtins)
LOG: Parsing C:\Users\user\venv\lib\mypy\typeshed\stdlib\3\sys.pyi (sys)
LOG: Parsing C:\Users\user\venv\lib\mypy\typeshed\stdlib\3\abc.pyi (abc)
LOG: Parsing C:\Users\user\venv\lib\mypy\typeshed\stdlib\3\types.pyi (types)
LOG: Parsing C:\Users\user\venv\lib\mypy\typeshed\stdlib\3_importlib_modulespec.pyi (_importlib_modulespec)
LOG: Loaded graph with 7 nodes
LOG: Found 2 SCCs; largest has 6 nodes
LOG: Processing SCC of size 6 (_importlib_modulespec types abc typing sys builtins) as inherently stale
LOG: Processing SCC singleton (mypy_bug) as inherently stale
LOG: No fresh SCCs left in queue
LOG: Build finished in 0.579 seconds with 7 modules, 1708 types, and 1 errors
mypy_bug.py:5: error: List item 0 has incompatible type "Tuple[Tuple[int, int], str]"

The line no 5 is recognised correctly as incompatible with Dict[str, int]
But line no 4 is not recognised, but in my opinion Dict[str, int] != Dict[Tuple[str, int], str]

@gvanrossum
Copy link
Member

Thanks! I can repro this. It seems that because dict.update() is overloaded to accept either Dict[key, value] or Iterable[Tuple[key, value]], when it finds no match for the Dict overload, it uses the Iterable overload, and iterating over a dict produces its keys.

I suspect this is going to be somewhat tricky to fix without special-casing in mypy. @JukkaL, thoughts?

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 8, 2016

Perhaps overload resolution in mypy is wrong here. We could perhaps choose the target overload variant using erased types only (e.g. Dict[Any, Any] instead of Dict[str, int]). I think that that's how it used to behave, but maybe that would cause other sorts of trouble elsewhere. At least it's worth considering. If that doesn't work, then we'd probably have to special case, which is not entirely crazy for such an important class, but it's not a high-priority thing as this is a pretty unusual edge case.

@dmoisset
Copy link
Contributor

From what I saw this is unrelated to the overload of dict.update and has to do with the confusing error message shown in dict displays (which always adds an extra Tuple). I added a PR (#3100) to fix the error message and what you see in this example makes more sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

No branches or pull requests

4 participants