Mypy doesn't recognize relative imports as module names
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Reproducer in my_pkg/init.py und my_pkg/my_module.py und führe anschließend mypy my_pkg/ aus, um die gemeldeten Fehler zu bestätigen. Verfolge, wie der relative Import und der Wildcard-Import interpretiert werden, und definiere den Abschluss als das korrekte Erkennen des Moduls und seines __all__ ohne Fehler oder die Dokumentation der unterstützten Alternative.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100