Closed as not planned
Description
Bug Report
mypy
incorrectly reports a comparison-overlap
error when using Enum
or Literal
types with global variables. It seems mypy
is not able to correctly infer that the variables are global and their types can change, leading to incorrect narrowing and subsequent overlap errors. This behavior is not observed with other type unions like int | str
.
To Reproduce
import enum
from typing import Literal
l: Literal[1] | Literal[2] = 1
class E(enum.Enum):
VAL1 = 1
VAL2 = 2
e: E = E.VAL1
def f() -> None:
global e, l
e = E.VAL2
l = 2
def g() -> None:
assert l == 1
assert e == E.VAL1
f()
print(l == 2)
print(e == E.VAL2)
g()
Expected Behavior
Mypy should not report any comparison-overlap
errors in the provided code when run with --strict
. It should understand that l
and e
are global variables and their types can be reassigned within the f()
function, so the assertions and print statements in g()
are valid comparisons after f()
is called.
Actual Behavior
The following errors are reported:
/tmp/global_overlap.py:25: error: Non-overlapping equality check (left operand type: "Literal[1]", right operand type: "Literal[2]") [comparison-overlap]
/tmp/global_overlap.py:26: error: Non-overlapping equality check (left operand type: "Literal[E.VAL1]", right operand type: "Literal[E.VAL2]") [comparison-overlap]
Your Environment
- Mypy version used: 1.16.0 (compiled: yes)
- Mypy command-line flags:
--strict
- Mypy configuration options from
mypy.ini
(and other config files): No configuration file used. - Python version used: 3.11.10
- Pip list:
Package Version ----------------- ------- mypy 1.16.0 mypy-extensions 1.1.0 pathspec 0.12.1 typing-extensions 4.14.0