Open finite-state-machine opened 2 years ago
Note that the docs for the enum
module explicitly recommend that you do not access other members via self
in this way.
Note that the docs for the
enum
module explicitly recommend that you do not access other members viaself
in this way.
It seems that while this was the case in Python 3.10, since Python 3.11 there is no such recommendation anymore, and accessing members via other instances (including self
) is not only no longer deprecated but officially supported. The only recommendation is to avoid naming clashes between members and methods.
This bug also happens with literal values instead of auto()
:
import enum
class Planet(enum.Enum):
EARTH = 1
MARS = 2
JUPITER = 3
SATURN = 4
@property
def is_solid(self) -> bool:
return self in (self.EARTH, self.MARS)
@property
def is_home(self) -> bool:
return self is self.EARTH
home = Planet.EARTH
print(home)
print(home.MARS)
print(home.is_home)
print(home.is_solid)
$ python3.8 teste.py
Planet.EARTH
Planet.MARS
True
True
$ mypy teste.py
teste.py:12: error: Non-overlapping container check (element type: "Planet", container item type: "int") [comparison-overlap]
teste.py:16: error: Non-overlapping identity check (left operand type: "Planet", right operand type: "Literal[1]") [comparison-overlap]
Found 2 errors in 1 file (checked 1 source file)
$ mypy --version
mypy 1.4.1 (compiled: yes)
Btw, same errors if running as mypy --python-version 3.11 teste.py
I believe this is a duplicate of #10910
Bug Report
Within an
Enum
class e.g.,SomeEnum
, elements initialized withauto()
and accessed asself.SOME_ELEMENT
have typeenum.auto*
instead ofSomeEnum
, resulting in false positives for comparison operations.To Reproduce
Run mypy on the following code:
Expected Behavior
No errors should be issued.
Actual Behavior
A
comparison-overlap
error is issued on thereturn
statement:Additional notes
Changing the element references from e.g.
self.TENNIS
toGame.TENNIS
results in the expected behaviour.Your Environment
--strict
mypy.ini
(and other config files): none