python / python/mypy

Incorrect handling of `import from` namespace package that spans site-packages and the source tree

Offen
#14,822 1 Kommentar 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

The following command:

mypy --explicit-package-bases ns_package/sub_ns_package1/test.py

Resulted in

ns_package/sub_ns_package1/test.py:1: error: Module "ns_package" has no attribute "sub_ns_package2"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

With the following setup:

Source code tree:

ns_package
    sub_ns_package1
        test.py

(note that there is no __init__.py)

site-packages tree (partial):

ns_package
    sub_ns_package2
        module.py    

(same, no __init__.py)

test.py contents:

from ns_package import sub_ns_package2

While scratching my head I also found the following:

  • import sub_ns_package.ns_package2 works.
  • it also works if the above import appears together with from ns_package import sub_ns_package2

To Reproduce
See the above.

Expected Behavior

mypy should finish happily in the above example.

Actual Behavior

ns_package/sub_ns_package1/test.py:1: error: Module "ns_package" has no attribute "sub_ns_package2"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.0.1
  • Mypy command-line flags: --explicit-package-bases (tried with or without it) ; --namespace-packages (tried with or without it)
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.8.16

Potential Fix
I have very little knowledge about the codebase, but the following patch would make the problem go away. I'm not sure it's the right fix though.

https://github.com/python/mypy/commit/e69bbd2c82e60640171dd7e2653c010976ad4239

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 dem gezeigten namespace-package-Baum und mypy --explicit-package-bases ns_package/sub_ns_package1/test.py; untersuche anschließend den referenzierten Commit e69bbd2c82e60640171dd7e2653c010976ad4239. Füge eine Regressionstestabdeckung für den Fall eines aufgeteilten source-tree/site-packages hinzu und verifiziere, dass der Befehl ohne den attr-defined-Fehler abgeschlossen wird.

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

Neue Issues direkt in Ihr Postfach

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