Skip to content

Same variable with a union type is not checked in "lockstep" within an expression #6478

Open
@Hanaasagi

Description

@Hanaasagi

Test env:

  • mypy 0.670
  • Python 3.7.2

Test code

from typing import List, Union


def f(x: Union[int, List[int]]) -> None:  # `TypeVar` can fix it
    x + x

Run mypy with default option, will get

a.py:5: error: Unsupported operand types for + ("int" and "List[int]")
a.py:5: error: Unsupported operand types for + ("List[int]" and "int")
a.py:5: note: Both left and right operands are unions

But actually, both left and right operands are same variable. Only int + int or List[int] + List[int] here. It seems like mypy does not consider object identity. And I'm not sure this is a bug, because above code is meaningless.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions