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

Incompatible types in assignment for dict[key] = tmp = value and nested structures #5289

Open
pmhahn opened this issue Jun 28, 2018 · 1 comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low

Comments

@pmhahn
Copy link

pmhahn commented Jun 28, 2018

I have Python2 code building some nested structures like lists/tuples/sets/dicts in dicts. For populating the nested structure I keep a temporary reference, which mypy fails to parse if the (untyped) temporary reference is not the first variable being assigned:

#!/usr/bin/python2.7
from typing import Dict, Set  # noqa
x = {}  # type: Dict[str, Set[str]]
z = x[''] = set()  # okay
x[''] = y = set()  # fail: Incompatible types in assignment (expression has type "Set[<nothing>]", target has type "Set[str]")
@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-2-low false-positive mypy gave an error on correct code labels Jun 28, 2018
@ilevkivskyi
Copy link
Member

Hm, interesting, mypy processes the targets in a weird order, first lvalues[-1], then lvalues[:-1]. It infers the rvalue type only once, then uses it for subsequent targets. This logic is apparently broken when the first inference fails. Since y is a new variable and the set is empty, mypy can't infer the type correctly as in first case. A possible solution is to check if the previous inference failed (look for partial types and uninhabited types with ambiguous=True), and in this case attempt the next lvalue.

This however might need some refactoring, so I think this is relatively low priority, also there are workarounds: declare types of problematic variables upfront, instead of relying on inference, or use a single assignment target, or change their order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low
Projects
None yet
Development

No branches or pull requests

2 participants