A 'type only' module import should not count as importing a module
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Merge medio
- 1 d 18 h
- PR fusionados (30 d)
- 54
Descripción
given:
my-app/
├─ package/
│ ├─ __init__.py
│ ├─ things.py
├─ entry.py
├─ mod.py
entry.py:
import mod
import package
mod.func(package.things.Thing())
mod.py:
def func(it):
...
In typeshed/bundled stubs: mod.pyi
from package.things import Thing
def func(it: Thing) -> None: ...
OR, if this is fake imported in the .py module: mod.py
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING
# do a type import so doesn't cause circular imports or slow down loading
from package.things import Thing
def func(it: Thing) -> None:
...
package.things.py
class Thing:
...
When mypy --strict entry.py is executed, no error is generated.
When python entry.py is run: AttributeError: module 'package' has no attribute 'things'
Mypy treats the 'type import' as importing package.things, when in reality it isn't. I understand this is consistent with what TYPE_CHECKING should do, but seems really sus if you ask me.
This more seriously affects typeshed, where all imports are fake and mypy will think all fakely imported modules are really imported.
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 comportamiento con entry.py, mod.py, package/init.py y package/things.py utilizando los comandos proporcionados de mypy --strict y Python. Empieza rastreando cómo se trata la importación de solo tipos en mod.pyi o el bloque TYPE_CHECKING y, después, añade un caso de regresión que demuestre que package.things no debe estar disponible en tiempo de ejecución.
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
- Bien especificado
- Aptitud para principiantes
- 35/100