Skip to content

Wrong comparison-overlap with Enum/Literal when using globals #19283

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

Closed
llucax opened this issue Jun 12, 2025 · 1 comment
Closed

Wrong comparison-overlap with Enum/Literal when using globals #19283

llucax opened this issue Jun 12, 2025 · 1 comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@llucax
Copy link

llucax commented Jun 12, 2025

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
    
@brianschubert
Copy link
Collaborator

Thanks, this is related to / duplicate of #11969

@brianschubert brianschubert added bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder labels Jun 12, 2025
@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jun 13, 2025
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-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

3 participants