Skip to content

Commit ec45983

Browse files
authored
New semantic analyzer: add comments and refactor (#7130)
Document deferral logic in some detail and remove redundant calls to `defer()`. Work towards #7071.
1 parent 0ee6cbe commit ec45983

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

mypy/newsemanal/semanal.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,6 @@ def process_type_annotation(self, s: AssignmentStmt) -> None:
21822182
analyzed = self.anal_type(s.type, allow_tuple_literal=allow_tuple_literal)
21832183
# Don't store not ready types (including placeholders).
21842184
if analyzed is None or has_placeholder(analyzed):
2185-
self.defer()
21862185
return
21872186
s.type = analyzed
21882187
if (self.type and self.type.is_protocol and isinstance(lvalue, NameExpr) and
@@ -3565,7 +3564,6 @@ def analyze_type_application_args(self, expr: IndexExpr) -> Optional[List[Type]]
35653564
analyzed = self.anal_type(typearg, allow_unbound_tvars=True,
35663565
allow_placeholder=True)
35673566
if analyzed is None:
3568-
self.defer()
35693567
return None
35703568
types.append(analyzed)
35713569
return types
@@ -4507,9 +4505,23 @@ def anal_type(self,
45074505
third_pass: bool = False) -> Optional[Type]:
45084506
"""Semantically analyze a type.
45094507
4510-
Return None only if some part of the type couldn't be bound *and* it referred
4511-
to an incomplete namespace. In case of other errors, report an error message
4512-
and return AnyType.
4508+
Args:
4509+
typ: Type to analyze (if already analyzed, this is a no-op)
4510+
allow_placeholder: If True, may return PlaceholderType if
4511+
encountering an incomplete definition
4512+
third_pass: Unused; only for compatibility with old semantic
4513+
analyzer
4514+
4515+
Return None only if some part of the type couldn't be bound *and* it
4516+
referred to an incomplete namespace or definition. In this case also
4517+
defer as needed. During a final iteration this won't return None;
4518+
instead report an error if the type can't be analyzed and return
4519+
AnyType.
4520+
4521+
In case of other errors, report an error message and return AnyType.
4522+
4523+
NOTE: The caller shouldn't defer even if this returns None or a
4524+
placeholder type.
45134525
"""
45144526
a = self.type_analyzer(tvar_scope=tvar_scope,
45154527
allow_unbound_tvars=allow_unbound_tvars,

mypy/newsemanal/typeanal.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ def no_subscript_builtin_alias(name: str, propose_alt: bool = True) -> str:
9494
class TypeAnalyser(SyntheticTypeVisitor[Type], TypeAnalyzerPluginInterface):
9595
"""Semantic analyzer for types.
9696
97-
Converts unbound types into bound types.
97+
Converts unbound types into bound types. This is a no-op for already
98+
bound types.
99+
100+
If an incomplete reference is encountered, this does a defer. The
101+
caller never needs to defer.
98102
"""
99103

100104
# Is this called from an untyped function definition?

0 commit comments

Comments
 (0)