Skip to content

Unexpected comparison-overlap error for identity comparison of optionals #14395

Open
@cebtenzzre

Description

@cebtenzzre

Bug Report

mypy generates "error: Non-overlapping identity check" for identity comparison between optionals, even though None has the same identity as None. This is especially confusing for the x is y is None pattern, which I would expect to be valid as long as x and y are optional.

To Reproduce

def foo(x: int | None, y: str | None) -> None:
    if x is y:
        print('x and y have same identity!')

foo(None, None)

Actual Behavior

$ python repr.py
x and y have same identity!

$ mypy --strict repr.py
repr.py:2: error: Non-overlapping identity check (left operand type: "Optional[int]", right operand type: "Optional[str]")  [comparison-overlap]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-overlapOverlapping equality check

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions