Skip to content

False positive for "Non-overlapping identity check" when comparing Enum variants #10910

Open
@kmurphy4

Description

@kmurphy4

Bug Report

I think I've uncovered a false positive in mypy's type-checking of is operator with Enums. See below for details.

I know that Enums are a bit funky, so it's possible this is expected behavior (actually, there's a related bug report here #9817).

To Reproduce

# test.py
from enum import Enum

class Food(Enum):
    SPAM = "spam"
    HAM = "ham"

    def is_spam(self) -> bool:
        return self is self.SPAM

print(Food.SPAM.is_spam())
print(Food.HAM.is_spam())
$ python3 test.py
True
False
$ mypy test.py
test.py: note: In member "is_spam" of class "Food":
test.py:8:16: error: Non-overlapping identity check (left operand type: "Food", right operand type: "str") 
[comparison-overlap]
            return self is self.SPAM
                   ^
Found 1 error in 1 file (checked 1 source file)

This goes away if I write it as

def is_spam(self) -> bool:
    return self is Food.SPAM

though I'd like to avoid having to duplicate the name of the class a bunch of times in methods with lots of branching on the variant.

Your Environment
$ mypy --version
mypy 0.910
$ cat .mypy.ini
[mypy]
# https://mypy.readthedocs.io/en/stable/index.html

python_version = 3.6

pretty = True
show_column_numbers = True
show_error_codes = True
show_error_context = True

check_untyped_defs = True
disallow_any_generics = True
disallow_incomplete_defs = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
disallow_untyped_defs = True
no_implicit_optional = True
no_implicit_reexport = True
strict_equality = True
warn_redundant_casts = True
warn_return_any = True
warn_unused_configs = True
warn_unused_ignores = True
$ python3 --version
Python 3.6.9
$ lsb_release -ds
Ubuntu 18.04.5 LTS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions