Skip to content

Certain order of union members causes Incompatible types error #3085

Closed

Description

@Stiivi

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

Activity

  1. gvanrossum commented on Mar 30, 2017

    @gvanrossum
    Member

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

  2. Stiivi commented on Mar 30, 2017

    @Stiivi
    Author

    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.

  3. JelleZijlstra commented on Mar 30, 2017

    @JelleZijlstra
    Member

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

  4. ilevkivskyi commented on Mar 30, 2017

    @ilevkivskyi
    Member

    I think this is essentially a duplicate of #1850

  5. JukkaL commented on Mar 30, 2017

    @JukkaL
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions