python / python/mypy

Mypy doesn't recognize relative imports as module names

Aperta
#10,967 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-runtime-semantics
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il riproduttore in my_pkg/init.py e my_pkg/my_module.py, quindi esegui mypy my_pkg/ per confermare gli errori segnalati. Traccia il modo in cui vengono interpretati l’import relativo e l’import wildcard, e definisci come completato il riconoscimento corretto del modulo e del suo __all__ senza errori, oppure la documentazione dell’alternativa supportata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.