python / python/mypy

Mypy reports a function is "missing return statement" when unused imports are removed

Abierto
#13,667 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

I know this sounds ridiculous, but I'm not sure how else to explain it. In the middle of a large refactor of code, which can be found here in its original, mypy-passing state:

https://github.com/mesonbuild/meson/blob/83d18d137dc50437a8263983e8883198c7bc41ed/mesonbuild/compilers/detect.py#L272-L352

I have a function which looks like this:

def _handle_exceptions(data: StuffICareAbout) -> typing.NoReturn:
    raise mesonlib.EnvironmentException(f'{data} happened and this is bad')

def detect_static_linker(env: 'Environment', compiler: Compiler) -> StaticLinker:
    data = do_stuff()
    if foo_condition:
        return StaticLinkerA()
    elif bar_condition:
        return StaticLinkerB()
    _handle_exceptions(data)

mypy says this is fine. detect_static_linker() doesn't always return, but if it doesn't return, it runs a function that is guaranteed to never return, always raise.

This somehow stops working during the course of this series of changes, which doesn't affect the high-level overview of that function: https://github.com/eli-schwartz/meson/compare/83d18d137dc50437a8263983e8883198c7bc41ed...681609624b687d660b234a9393d446a8f2f6663b

After way too much troubleshooting, and separating out parts of the commit to see what caused it, I produced the above series of commits, and...

... this commit is fingered by git bisect: https://github.com/eli-schwartz/meson/compare/fe42a1cadade94cb39e2761500a1f5849ad7d2e8...b1676e57f60c6a1fb7533096402ae5c8ff713a35

All it does is finally drop the unused imports rendered redundant by the previous commit. I don't understand how this could be possible.

Furthermore, if I run mypy mesonbuild/compilers/detect.py, mypy passes. If I run ./run_mypy.py, a script that contains a list of files to check, and runs mypy "list" "of" "files", it triggers the issue.

Discussion in the meson review can be seen here, in case that's interesting (two people failed to reproduce it locally at first): https://github.com/mesonbuild/meson/pull/10810#discussion_r970164045

Your Environment

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza reproduciendo la diferencia entre mypy mesonbuild/compilers/detect.py y ./run_mypy.py, usando Python 3.10, mypy 0.971 y .mypy.ini. Compara las revisiones mencionadas e inspecciona mesonbuild/compilers/detect.py junto con run_mypy.py; se considerará terminado cuando se explique por qué eliminar imports no utilizados cambia la comprobación agregada y se corrija el diagnóstico incoherente.

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
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.