python / python/mypy

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

Aperta
#14,822 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

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

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

Riproduci il report con l’albero namespace-package mostrato e mypy --explicit-package-bases ns_package/sub_ns_package1/test.py; quindi esamina il commit referenziato e69bbd2c82e60640171dd7e2653c010976ad4239. Aggiungi una copertura di regressione per il caso di source-tree/site-packages suddiviso e verifica che il comando termini senza l’errore attr-defined.

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.