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

Certain order of union members causes Incompatible types error #3085

Closed
Stiivi opened this issue Mar 30, 2017 · 5 comments
Closed

Certain order of union members causes Incompatible types error #3085

Stiivi opened this issue Mar 30, 2017 · 5 comments

Comments

@Stiivi
Copy link

Stiivi commented Mar 30, 2017

I’m getting this weird error: Incompatible types in assignment (expression has type Dict[str, Union[str, float]], variable has type Dict[str, Union[str, int, float, bool]])

Value = Union[str, int, float, bool]
d: Dict[str, Value]
d = {}

however this works:

Value = Union[str, float, bool, int]

The order of union elements is different.

Other offenses:

Value = Union[int, str, bool, float]
Value = Union[str, int, bool, float]

Causes: Incompatible types in assignment (expression has type Dict[str, Union[str, float]], variable has type Dict[str, Union[int, str, bool, float]])

@gvanrossum
Copy link
Member

Might have been fixed by #3025, which got rolled back for other reasons.

@Stiivi
Copy link
Author

Stiivi commented Mar 30, 2017

Another observation that looks related is this incorrect error message for the last statement of the following example. The last statement should cause Incompatible types and it does:

from typing import Union, Dict, Optional

Value = Union[str, float, bool, int]
d: Dict[str, Value]
d = {}

v: Value
v = d.get("athing")

However the error is showing wrong types:

Incompatible types in assignment (expression has type "Union[str, float, bool, None]",
                                    variable has type "Union[str, float, bool, int]")

(indented for better demonstration)

The first union (expression type) in the error should have five elements – it is missing int.

@JelleZijlstra
Copy link
Member

That's probably also something deciding that int is compatible with float, so Union[float, int] == float.

@ilevkivskyi
Copy link
Member

I think this is essentially a duplicate of #1850

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 30, 2017

This should be fixed by #3025 (I'll need to double check) and yes, this looks like a duplicate of #1850 so I'm closing this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants