python / python/mypy

Circular "import *" causes crash

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

Dieses Issue hat noch niemand übernommen.

crash topic-import-cycles
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beschreibung

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

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 Absturz mit den Projektdateien und mypy.ini aus Exp2.zip und verwende log.txt und log2.txt, um die fehlschlagenden und erfolgreichen Durchläufe zu vergleichen. Lies den Pfad zur Importbehandlung in semanal.py, insbesondere die Aufrufe von add_imported_symbol und add_unknown_imported_symbol während der letzten Iteration. Als erledigt gilt die Aufgabe, wenn der zirkuläre Import mypy nicht mehr zum Absturz bringt und der fehlende importierte Name die vorgesehene Behandlung erhält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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