Skip to content

Mypy error on assigning an empty dict to Union variable #1458

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
sharmaeklavya2 opened this issue May 1, 2016 · 1 comment
Closed

Mypy error on assigning an empty dict to Union variable #1458

sharmaeklavya2 opened this issue May 1, 2016 · 1 comment
Assignees
Labels
bug mypy got something wrong

Comments

@sharmaeklavya2
Copy link

This fails:

d = {} # type: Union[Dict[str, int], List[int]]

with error message:

error: Incompatible types in assignment (expression has type Dict[None, None],
variable has type "Union[Dict[str, int], List[int]]")

This also fails with a similar error message:

d = [] # type: Union[Dict[str, int], List[int]]
error: Incompatible types in assignment (expression has type List[None],
variable has type "Union[Dict[str, int], List[int]]")

This works however:

d = {"abc":123} # type: Union[Dict[str, int], List[int]]

I was expecting empty dict to work with Union, since the following 2 statements work:

  • d = {} # type: Dict[str, int]
  • d = 3 # type: Union[int, str]
@rwbarton rwbarton self-assigned this May 1, 2016
@rwbarton rwbarton added this to the 0.4 milestone May 1, 2016
@rwbarton
Copy link
Contributor

rwbarton commented May 1, 2016

This is related to inference for unions like #1430 (but simpler because a Dict is not a List). I'll add support for this case to #1408.

@gvanrossum gvanrossum added the bug mypy got something wrong label May 1, 2016
rwbarton added a commit to rwbarton/mypy that referenced this issue May 3, 2016
The old code always generated a conjunction, where we sometimes want a
disjunction.  Since we can't represent or solve a disjunction of
constraints, need to approximate.

In the presence of Unions, the constraints inferred from (template,
actual, direction) are not necessarily the reverses of the constraints
inferred from (template, actual, neg_op(direction)). The
visit_instance and visit_callable cases were updated accordingly. Test
testGenericFunctionSubtypingWithUnions fails without this change.

Fixes python#1458 and the remaining part of python#1241.
rwbarton added a commit to rwbarton/mypy that referenced this issue May 3, 2016
The old code always generated a conjunction, where we sometimes want a
disjunction.  Since we can't represent or solve a disjunction of
constraints, need to approximate.

In the presence of Unions, the constraints inferred from (template,
actual, direction) are not necessarily the reverses of the constraints
inferred from (template, actual, neg_op(direction)). The
visit_instance and visit_callable cases were updated accordingly. Test
testGenericFunctionSubtypingWithUnions fails without this change.

Fixes python#1458 and the remaining part of python#1241.
@JukkaL JukkaL closed this as completed in 7c37519 May 4, 2016
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

3 participants