python / python/mypy

fails to import module from source directory for sources without .py extension

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

Descripción

To Reproduce

Given the files:

testdir/integration-tests/common.py

class CommonConfig:
    pass

testdir/integration-tests/test1

#!/usr/bin/env python3
import common
assert __name__ == '__main__'
config = common.CommonConfig()

If I run mypy from testdir/ on integration-tests/test1, it fails to find the import common from the same directory:

$ mypy integration-tests/test1
integration-tests/test1:3: error: Cannot find implementation or library stub for module named "common"  [import-not-found]
integration-tests/test1:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I expect mypy to be able to find the common module from the directory containing the source file, and thus to exit without errors:

$ mypy integration-tests/test1

IIUC, it should find the import, since the search path should include: "The directories containing the sources given on the command line"

Other notes

If I rename test1 to test1.py, it works as expected.

If I run mypy from testdir/integration-tests instead of from testdir (such that common.py is in the current working directory), it works as expected.

Environment

$ python3 --version
Python 3.10.12
$ mypy --version
mypy 1.19.1 (compiled: yes)

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 problema con testdir/integration-tests/common.py y testdir/integration-tests/test1 sin extensión, y luego ejecuta mypy desde testdir. Rastrea cómo mypy construye su ruta de búsqueda para un archivo fuente sin extensión .py; se considera terminado cuando se encuentra el import common y mypy termina sin errores, mientras los casos con .py y con un directorio de trabajo alternativo siguen funcionando.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
tooling
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 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.