python / python/mypy

Mypy doesn't recognize relative imports as module names

Abierto
#10,967 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-runtime-semantics
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 doesn't seem to recognize a relative import as a module-type.

To Reproduce

Given a sample package with the following structure

my_pkg/
├── __init__.py
└── my_module.py

__init__.py:

import types
from typing import List

from .my_module import *

__all__ = []
submodules: List[types.ModuleType] = [my_module]
for submodule in submodules:
    __all__.extend(submodule.__all__)

my_module.py:

__all__ = ["my_var"]

my_var = "hello!"

mypy output:

$ mypy my_pkg/
my_pkg/__init__.py:7: error: Name "my_module" is not defined
my_pkg/__init__.py:9: error: Module has no attribute "__all__"
Found 2 errors in 1 file (checked 2 source files)

Expected Behavior

I expected to see no errors, as the python is valid code.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.10
  • Operating system and version: Ubuntu 20.04.2

Additional Info
Because the above is an odd pattern, I'd like to explain it.

Imagine I started with an app and a single module, my_module. I'd then use access the module inside the app like so

from my_app import my_module

print(my_module.my_var)

Now, as time goes on, my_module grows increasingly complex, so I decide to split up my_module into a package, my_pkg. Unfortunately, we now have to change our imports in the app:

from my_app.my_pkg import my_module

This isn't desirable, because any code that was using my_module now has to change its imports. To combat this, I name my_pkg the same name as my_module and include the above snippet of code in the __init__.py. Now, to the application, the fact that my_module was split into a package is completely abstracted and the import syntax remains the same:

from my_app import my_pkg

print(my_pkg.my_var)

Note, my_pkg would be have the same name as the original module.

I don't necessarily care that mypy doesn't deal with my above code well, but I do care that the desired behavior is supported by mypy somehow. If there is a better way to do what I've explained, I am all ears.

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 con el reproductor en my_pkg/init.py y my_pkg/my_module.py; después ejecuta mypy my_pkg/ para confirmar los errores reportados. Traza cómo se interpretan la importación relativa y la importación wildcard, y define como terminado que el módulo y su __all__ se reconozcan correctamente sin errores, o que se documente la alternativa compatible.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.