mypy cannot detect class attribute change if asserted but modified in external 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
Bug Report
mypy cannot detect class attribute change if assert but modified in external call
This is a case found when I'm trying to check the close status first, and after some logic, close it then.
The print(1) in f1() is reachable, but mypy think Statement is unreachable because we asserted assert not self._closed first, however, it was changed in the close() call
For the f2() we modify the self._close inplace, so mypy is happy with it.
I think it may be hard and not worth to fix, just reported to let you know. :)

To Reproduce
save the code and then run mypy --warn-unreachable assert_but_change.py
# assert_but_change.py
class Foo:
def __init__(self) -> None:
self._closed = False
def f1(self) -> None:
assert not self._closed
# do some logic
self.close()
if self._closed:
print(1)
def close(self) -> None:
self._closed = True
def f2(self) -> None:
assert not self._closed
self._closed = True
if self._closed:
print(1)
Expected Behavior
the print(1) in f1() is reachable, mypy should not complain
just like the print(1) in f2()
Actual Behavior
Statement is unreachable
Your Environment
- Mypy version used: mypy 0.971
- Mypy command-line flags: --warn-unreachable
- Mypy configuration options from
mypy.ini(and other config files): No - Python version used: cpython 3.9.4
- Operating system and version: windows 10
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
Comienza con la reproducción proporcionada en assert_but_change.py y ejecuta mypy --warn-unreachable contra ella. Rastrea cómo se analizan la aserción, la llamada externa a close() y la comprobación posterior de self._closed; después, verifica que f1() ya no informe de print(1), que es alcanzable, como inalcanzable, mientras que f2() siga siendo válida.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- 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