python / python/mypy

Relative imports in stub modules checking live code

Abierto
#12,102 0 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
Merge medio
1 d 18 h
PR fusionados (30 d)
54

Descripción

Bug Report

Hi all! I'm trying to get to the bottom of this issue with stub imports, and I can't even find information to tell me if this is actually expected behaviour. Please see repro.

To Reproduce

A quick setup running this series of shell commands follows:

>>> $ mkdir module
>>> $ mkdir module-stubs
>>> $ touch module/__init__.py
>>> $ touch module-stubs/submodule.pyi
>>> $ echo "from . import submodule" > module-stubs/__init__.pyi
>>> $ echo "import module" > run.py
>>> $ mypy run.py 
module-stubs/__init__.pyi:1: error: Module 'module' has no attribute 'submodule'
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I would have expected that an import within the stub module still would understand that the stub module exists, and use the stub submodule to verify the import.
The real module can be doing any number of dynamic things during the import to ensure "submodule" is available.

OR this is expected behaviour and I need to adjust my expectations.

Actual Behavior

The result appears to be that mypy goes looking in the actual python package for the module and not within the stub.
module-stubs/__init__.pyi:1: error: Module 'module' has no attribute 'submodule'

Actually creating the submodule in the python package removes this error.

Your Environment

Mypy versions tested on 0.931 and 0.812 with same results.
OS version: linux mint 20.3

Thanks a lot everyone.

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

Reproduce el informe usando los comandos de shell del issue, especialmente module-stubs/init.pyi, module-stubs/submodule.pyi y run.py. Empieza rastreando cómo mypy resuelve la importación relativa en el paquete de stubs; estará terminado cuando el comportamiento esté documentado o la importación se resuelva de forma coherente con la semántica prevista del módulo stub.

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
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.