Skip to content

False positive comparison-overlap #15509

Open
@fabi321

Description

@fabi321

Bug Report

If a class variable is changed in a function after a check, mypy assumes that all variables are still the same. This leads to a comparison-overlap error, where none is.

To Reproduce

from enum import Enum

class Types(Enum):
    A: str = "A"
    B: str = "B"

class A:
    def __init__(self) -> None:
        self.type: Types = Types.A
    
    def change_type(self) -> None:
        self.type = Types.B
    
    def test(self) -> None:
        if self.type == Types.A:
            self.change_type()
            if self.type == Types.B:
                print("Should be the case")

Real world example

Expected Behavior

This is perfectly fine python, and if run in the interpreter, Should be the case is printed.

Actual Behavior

main.py:17:16: error: Non-overlapping equality check (left operand type: "Literal[Types.A]", right operand type: "Literal[Types.B]")  [comparison-overlap]
main.py:18:17: error: Statement is unreachable  [unreachable]

Your Environment

playground link

  • Mypy version used: 1.4.0 (bug is not present in 1.3.0)
  • Mypy command-line flags: --strict
  • Python version used: 3.10

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