python / python/mypy

Circular "import *" causes crash

Aperta
#12,568 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

crash topic-import-cycles
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

In my project I have a number of modules that do from ... import * from other modules, and there happens to be a circularity of these imports. When I run mypy, it crashes.
I have attached a zip containing the files in the project, the mypy.ini, and all the output (log.txt).
There are numerous log messages which I have added to my copy of mypy code, to help make it clear what is going on. To wit, my modules form an SCC, and when analyzing that set of modules, it goes through 3 iterations. In the second iteration, it can make no progress because it cannot determine what names to import in each case. In the third and final iteration, it finds that it can resolve some otherwise unresolved symbols, due to clearing the list of incomplete modules.
However, the new names being added are undefined because they are missing from the module being imported from. Thus it makes a placeholder node for the symbol table.
Later on, when trying to serialize these modules, it rejects the placeholders and raises an assertion error.

This missing imported name, I think, should be included in the importing module, but it should be treated as an unknown import.

I made a change to my mypy code, by calling add_unknown_imported_symbol instead of add_imported_symbol. mypy now runs successfully.

Specifically, I changed

					self.add_imported_symbol(name, node, i,
												 module_public=module_public,
												 module_hidden=not module_public)

to

					if self.final_iteration and isinstance(node.node, PlaceholderNode):
						self.add_unknown_imported_symbol(name, i, fullname, 
														 module_public=module_public,
														 module_hidden=not module_public)
					else:
						self.add_imported_symbol(name, node, i,
												 module_public=module_public,
												 module_hidden=not module_public)

in semanal.py.

I'm not sure if adding the symbol as an unknown import is what you want to do. One other possibility would be to leave the symbol out of the module symbol table, and issuing a similar error message.

Attached is a zip file with:

  • My project files
  • mypy.ini
  • log.txt, the output from the failed run.
  • log2.txt, the output from the successful run after the change I made.
    Exp2.zip

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 crash con i file del progetto e mypy.ini di Exp2.zip, usando log.txt e log2.txt per confrontare le esecuzioni che falliscono e quelle che hanno successo. Leggi il percorso di gestione degli import in semanal.py, in particolare le chiamate a add_imported_symbol e add_unknown_imported_symbol durante l'iterazione finale. Il lavoro è completato quando l'import circolare non causa più il crash di mypy e il nome importato mancante riceve la gestione prevista.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.