Non-overlapping identity check when member variable mutated by function call
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Reproduce el informe de test.py usando mypy 0.780 con --strict, centrándote en do_thing y _set_my_value_to_bar. Rastrea cómo se conserva la comprobación del valor del miembro a través de la llamada al método; se considera terminado cuando la comprobación posterior de identidad de MyEnum.BAR ya no se notifica como no solapada cuando la llamada puede mutar el miembro.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers, tooling
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100