Skip to content

Commit 560888d

Browse files
authored
New semantic analyzer: Various small updates (#6246)
These respond to some review feedback in #6240.
1 parent 8afda22 commit 560888d

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

mypy/newsemanal/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2646,7 +2646,7 @@ def analyze_types(self, items: List[Expression]) -> List[Type]:
26462646
if analyzed is not None:
26472647
result.append(analyzed)
26482648
else:
2649-
# TODO: Is this the right thing to do?
2649+
# TODO: Is this the right thing to do? Or maybe return Optional[List[Type]]?
26502650
result.append(AnyType(TypeOfAny.from_error))
26512651
except TypeTranslationError:
26522652
self.fail('Type expected', node)

mypy/newsemanal/semanal_main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@
4040

4141

4242
def semantic_analysis_for_scc(graph: 'Graph', scc: List[str]) -> None:
43-
# Assume reachability analysis has already been performed.
43+
"""Perform semantic analysis for all modules in a SCC (import cycle).
44+
45+
Assume that reachability analysis has already been performed.
46+
"""
47+
# Note that functions can't define new module-level attributes
48+
# using 'global x', since module top levels are fully processed
49+
# before functions. This limitation is unlikely to go away soon.
4450
process_top_levels(graph, scc)
4551
process_functions(graph, scc)
4652

mypy/plugin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@ def anal_type(self, t: Type, *,
182182
allow_unbound_tvars: bool = False,
183183
report_invalid_types: bool = True,
184184
third_pass: bool = False) -> Optional[Type]:
185-
"""Analyze an unbound type."""
185+
"""Analyze an unbound type.
186+
187+
Return None if the some part of the type is not ready yet (only
188+
happens with the new semantic analyzer). In this case the current
189+
target being analyzed will be deferred and analyzed again.
190+
"""
186191
raise NotImplementedError
187192

188193
@abstractmethod

test-data/unit/check-newsemanal.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ from b import bad # E: Module 'b' has no attribute 'bad'
8080
[file b.py]
8181
from a import bad2 # E: Module 'a' has no attribute 'bad2'
8282

83+
[case testNewAnalyzerTypeAnnotationCycle4]
84+
# flags: --new-semantic-analyzer
85+
import b
86+
[file a.py]
87+
from b import bad # E: Module 'b' has no attribute 'bad'
88+
[file b.py]
89+
# TODO: Could we generate an error here as well?
90+
from a import bad
91+
8392
[case testNewAnalyzerSimpleFunction]
8493
# flags: --new-semantic-analyzer
8594
def f(x: int) -> str:

0 commit comments

Comments
 (0)