python / python/mypy

Wrong type inferred for a name conflicting with a module name in an import cycle

Aperta
#7,419 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug priority-1-normal semantic-analyzer topic-import-cycles
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

The following is a failing test case which demonstrates the issue. In runtime, the type of a that is printed is str, however mypy reveals the type _importlib_modulespec.ModuleType.

[case testImportCycleWithOverriddenName]
# flags: --strict --no-incremental
import package

[file package/__init__.py]
a = 'a from __init__'
from .b import B

[file package/a.py]

[file package/b.py]
from . import a
class B: pass
print(type(a))
reveal_type(a)

[out]
package/b.py:4: note: Revealed type is 'builtins.str'

Note that if the two lines in __init__.py are swapped, the runtime type is indeed ModuleType.

I did not look at the code, but my uneducated guess is that the current "state" of __init__.py is not consulted when the from . import a in b.py is processed. This can probably be reduced further.

  • mypy 0.720
  • Python 3.7.4

Spawned from issue #7393.

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 caso fornito testImportCycleWithOverriddenName ed eseguilo con --strict --no-incremental. Confronta package/init.py, package/a.py e package/b.py, prestando particolare attenzione all’ordine degli import e all’istruzione from . import a. È completato quando reveal_type(a) restituisce builtins.str, preservando al contempo il risultato ModuleType quando le due righe di init.py vengono scambiate.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.