Circular "import *" causes crash
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.inilog.txt, the output from the failed run.log2.txt, the output from the successful run after the change I made.
Exp2.zip
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the crash with the project files and mypy.ini from Exp2.zip, using log.txt and log2.txt to compare the failing and successful runs. Read the import-handling path in semanal.py, especially the add_imported_symbol and add_unknown_imported_symbol calls during the final iteration. Done means the circular import no longer crashes mypy and the missing imported name receives the intended handling.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100