Non-overlapping identity check when member variable mutated by function call
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
I get the following error in a situation where I think I shouldn’t:
test.py:25: error: Non-overlapping identity check (left operand type: "Literal[MyEnum.FOO]", right operand type: "Literal[MyEnum.BAR]")
Given the following code:
import enum
@enum.unique
class MyEnum(enum.Enum):
FOO = enum.auto()
BAR = enum.auto()
class MyClass:
__slots__ = (
"_my_value",
)
_my_value: MyEnum
def __init__(self) -> None:
super().__init__()
self._my_value = MyEnum.FOO
def do_thing(self) -> None:
if self._my_value is MyEnum.FOO:
self._set_my_value_to_bar()
# self._my_value = MyEnum.BAR
if self._my_value is MyEnum.BAR:
print("And yet, the value was BAR!")
def _set_my_value_to_bar(self) -> None:
self._my_value = MyEnum.BAR
if __name__ == "__main__":
x = MyClass()
x.do_thing()
If I uncomment the commented line, the error goes away. It seems that Mypy does not understand that a method call might mutate member variables of the class, obsoleting any prior checks of their values.
This is when running version 0.780 with --strict.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci il report di test.py usando mypy 0.780 con --strict, concentrandoti su do_thing e _set_my_value_to_bar. Traccia come il controllo del valore del membro viene mantenuto attraverso la chiamata al metodo; il lavoro è completato quando il successivo controllo di identità di MyEnum.BAR non viene più segnalato come non sovrapposto quando la chiamata può mutare il membro.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, tooling
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100