Skip to content

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

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

Open
cebtenzzre opened this issue Jan 4, 2023 · 1 comment
Open
Labels
bug mypy got something wrong topic-overlap Overlapping equality check

Comments

@cebtenzzre
Copy link

cebtenzzre commented Jan 4, 2023

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
@cebtenzzre cebtenzzre added the bug mypy got something wrong label Jan 4, 2023
@ichard26 ichard26 added the topic-overlap Overlapping equality check label Jan 15, 2023
@chnmasta05
Copy link

chnmasta05 commented May 31, 2023

It was decided when --strict-equality was implemented that (according to #6370) "Optional[X] and Optional[Y] are non-overlapping if X and Y are non-overlapping".

In my opinion, the documented reason "although technically None is overlap, it is most likely an error" is not very convincing, and we would all benefit from the change if the corner case was removed.

I'm not sure if I understand correctly, but this restriction was originally added to account for type promotions? Now that we don't ignore promotions anymore (#6674), we really should change this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-overlap Overlapping equality check
Projects
None yet
Development

No branches or pull requests

3 participants