Skip to content

Commit 52e41f8

Browse files
authored
Fix reporting of likely hangs in the new semantic analyzer (#6872)
Previously things like `class A(A): pass` resulted in a crash instead of an error message. Now we generate an error message with some context (but still not enough). Partially addresses #6495.
1 parent c9d35c4 commit 52e41f8

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

mypy/newsemanal/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def adjust_public_exports(self) -> None:
408408

409409
@contextmanager
410410
def file_context(self, file_node: MypyFile, fnam: str, options: Options,
411-
active_type: Optional[TypeInfo],
411+
active_type: Optional[TypeInfo] = None,
412412
scope: Optional[Scope] = None) -> Iterator[None]:
413413
# TODO: Use this above in visit_file
414414
scope = scope or self.scope

mypy/newsemanal/semanal_main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ def process_top_levels(graph: 'Graph', scc: List[str], patches: Patches) -> None
148148
while worklist:
149149
iteration += 1
150150
if iteration > MAX_ITERATIONS:
151-
state.manager.new_semantic_analyzer.report_hang()
151+
analyzer = state.manager.new_semantic_analyzer
152+
# Just pick some module inside the current SCC for error context.
153+
assert state.tree is not None
154+
with analyzer.file_context(state.tree, state.tree.path, state.options):
155+
analyzer.report_hang()
152156
break
153157
if final_iteration:
154158
# Give up. It's impossible to bind all names.

test-data/unit/check-newsemanal.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,3 +2238,8 @@ var1: Final = 1
22382238
def force1(x: Literal[1]) -> None: pass
22392239

22402240
force1(reveal_type(var1)) # E: Revealed type is 'Literal[1]'
2241+
2242+
[case testNewAnalyzerReportLoopInMRO]
2243+
class A(A): ...
2244+
[out]
2245+
main: error: Internal error: maximum semantic analysis iteration count reached

0 commit comments

Comments
 (0)