python / python/mypy

Relative imports in stub modules checking live code

Offen
#12,102 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere den Bericht mit den Shell-Befehlen im Issue, insbesondere mit module-stubs/init.pyi, module-stubs/submodule.pyi und run.py. Beginne damit nachzuverfolgen, wie mypy den relativen Import im Stub-Paket auflöst; abgeschlossen, wenn das Verhalten dokumentiert ist oder der Import konsistent mit der vorgesehenen Stub-Modul-Semantik aufgelöst wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.