-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(11064): fix crash due to undefined cyclic import *
#11346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(11064): fix crash due to undefined cyclic import *
#11346
Conversation
e4ff209
to
17268de
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR and the additional test cases!
First off, note that I don't understand the deferral parts of the semantic analyser very well :-) Because of my lack of familiarity, while it's clear to me how this avoids the assertion, it's hard for me to know whether this change is actually correct. For example, should we be changing this in add_symbol_table_node
itself, rather than just this one place?
Reading the code with that line of thinking got me this alternative patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
index 3c0d548b2..dc70d0918 100644
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -4567,7 +4567,12 @@ class SemanticAnalyzer(NodeVisitor[None],
names = self.current_symbol_table(escape_comprehensions=escape_comprehensions)
existing = names.get(name)
if isinstance(symbol.node, PlaceholderNode) and can_defer:
- self.defer(context)
+ if context is not None:
+ self.process_placeholder(name, 'name', context)
+ else:
+ # see note in docstring about None contexts
+ self.defer(context)
+
if (existing is not None
and context is not None
and not is_valid_replacement(existing, symbol)):
Reading the docstrings of defer
and process_placeholder
, I feel pretty good about that. The net effect should be pretty similar to your PR, but perhaps less specific to the exact issue in #11064 and ensures we'll get some kind of error message if we do hit that during the final iteration. It also avoids crashes in the test cases you added.
What do you think? :-)
Thanks for the suggestion! |
What's the changed behavior supposed to be for large codebases w/ some circular imports? Because I still get a deferral trace and the same message about checking being halted for errors. (when running mypy w/ this PR) |
Ah, Yeah I can collect logs, I don't know specifically what's causing it, it's a massive somewhat old (and unfortunately, closed source) codebase. I can look into it some more. |
I opened #11681 that builds on your work but uses that^ suggested diff. Thank you for making mypy better!! |
@hauntsaninja Thanks for picking up my commits. I hope your PR will get merged soon! |
Fixes #11064. Co-authored by @hi-ogawa Pushes #11346 over the finish line. Implements the suggestion in #11346 (review) Co-authored-by: Hiroshi Ogawa <[email protected]> Co-authored-by: hauntsaninja <>
Fixes python#11064. Co-authored by @hi-ogawa Pushes python#11346 over the finish line. Implements the suggestion in python#11346 (review) Co-authored-by: Hiroshi Ogawa <[email protected]> Co-authored-by: hauntsaninja <>
(Sorry for another draft PR. I wanted to see CI's results. I'll soon write a detail below)Sorry for stalling this PR so long. I updated the description below.
Description
Fixes #11064
It seems that the original issue has been fixed already (which corresponds to the test case
testCyclicUndefinedImportWithStar3
). However, the other two cases (testCyclicUndefinedImportWithStar1
andtestCyclicUndefinedImportWithStar2
) still cause crash, so this PR is indended to fix them.While working on the fix, first I tried to detect cyclic import during semanal (e.g. from
add_imported_symbol
), but in the end, it turns out simply settingcan_defer
produces sensible error message, so that's what this PR does.Test Plan
I added four tests related to cyclic import in
check-modules.test
:testCyclicUndefinedImportWithName
testCyclicUndefinedImportWithStar1
testCyclicUndefinedImportWithStar2
testCyclicUndefinedImportWithStar3
(replication of the linked issue)where
testCyclicUndefinedImportWithName
andtestCyclicUndefinedImportWithStar3
are already working in current master, and the other two are addressed in this PR.