Skip to content

Commit dff25d1

Browse files
committed
Rename semantic analyzer classes to SemanticAnalyzerPass[123]
Work towards #4083.
1 parent b98ebf0 commit dff25d1

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

mypy/build.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from typing import Deque
3232

3333
from mypy.nodes import (MypyFile, Node, ImportBase, Import, ImportFrom, ImportAll)
34-
from mypy.semanal import FirstPass, SemanticAnalyzer, ThirdPass
34+
from mypy.semanal import SemanticAnalyzerPass1, SemanticAnalyzerPass2, SemanticAnalyzerPass3
3535
from mypy.checker import TypeChecker
3636
from mypy.indirection import TypeIndirectionVisitor
3737
from mypy.errors import Errors, CompileError, DecodeError, report_internal_error
@@ -497,10 +497,11 @@ def __init__(self, data_dir: str,
497497
self.modules = {} # type: Dict[str, MypyFile]
498498
self.missing_modules = set() # type: Set[str]
499499
self.plugin = plugin
500-
self.semantic_analyzer = SemanticAnalyzer(self.modules, self.missing_modules,
500+
self.semantic_analyzer = SemanticAnalyzerPass2(self.modules, self.missing_modules,
501501
lib_path, self.errors, self.plugin)
502502
self.modules = self.semantic_analyzer.modules
503-
self.semantic_analyzer_pass3 = ThirdPass(self.modules, self.errors, self.semantic_analyzer)
503+
self.semantic_analyzer_pass3 = SemanticAnalyzerPass3(self.modules, self.errors,
504+
self.semantic_analyzer)
504505
self.all_types = {} # type: Dict[Expression, Type]
505506
self.indirection_detector = TypeIndirectionVisitor()
506507
self.stale_modules = set() # type: Set[str]
@@ -1605,7 +1606,7 @@ def patch_dependency_parents(self) -> None:
16051606
"""
16061607
In Python, if a and a.b are both modules, running `import a.b` will
16071608
modify not only the current module's namespace, but a's namespace as
1608-
well -- see SemanticAnalyzer.add_submodules_to_parent_modules for more
1609+
well -- see SemanticAnalyzerPass2.add_submodules_to_parent_modules for more
16091610
details.
16101611
16111612
However, this patching process can occur after `a` has been parsed and
@@ -1689,13 +1690,13 @@ def parse_file(self) -> None:
16891690
# definitions in the file to the symbol table. We must do
16901691
# this before processing imports, since this may mark some
16911692
# import statements as unreachable.
1692-
first = FirstPass(manager.semantic_analyzer)
1693+
first = SemanticAnalyzerPass1(manager.semantic_analyzer)
16931694
with self.wrap_context():
16941695
first.visit_file(self.tree, self.xpath, self.id, self.options)
16951696

16961697
# Initialize module symbol table, which was populated by the
16971698
# semantic analyzer.
1698-
# TODO: Why can't FirstPass .analyze() do this?
1699+
# TODO: Why can't SemanticAnalyzerPass1 .analyze() do this?
16991700
self.tree.names = manager.semantic_analyzer.globals
17001701

17011702
# Compute (direct) dependencies.

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def check_call(self, callee: Type, args: List[Expression],
538538
if (isinstance(callable_node, RefExpr)
539539
and callable_node.fullname in ('enum.Enum', 'enum.IntEnum',
540540
'enum.Flag', 'enum.IntFlag')):
541-
# An Enum() call that failed SemanticAnalyzer.check_enum_call().
541+
# An Enum() call that failed SemanticAnalyzerPass2.check_enum_call().
542542
return callee.ret_type, callee
543543

544544
if (callee.is_type_obj() and callee.type_object().is_abstract

mypy/nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def deserialize(cls, data: JsonDict) -> 'MypyFile':
255255
class ImportBase(Statement):
256256
"""Base class for all import statements."""
257257

258-
is_unreachable = False # Set by semanal.FirstPass if inside `if False` etc.
258+
is_unreachable = False # Set by semanal.SemanticAnalyzerPass1 if inside `if False` etc.
259259
is_top_level = False # Ditto if outside any class or def
260260
is_mypy_only = False # Ditto if inside `if TYPE_CHECKING` or `if MYPY`
261261

@@ -2433,7 +2433,7 @@ def serialize(self, fullname: str) -> JsonDict:
24332433
for key, value in self.items():
24342434
# Skip __builtins__: it's a reference to the builtins
24352435
# module that gets added to every module by
2436-
# SemanticAnalyzer.visit_file(), but it shouldn't be
2436+
# SemanticAnalyzerPass2.visit_file(), but it shouldn't be
24372437
# accessed by users of the module.
24382438
if key == '__builtins__':
24392439
continue

mypy/semanal.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,27 @@
1010
defines a new variable, the type of which is to be inferred (in a
1111
later pass; type inference or type checking is not part of semantic
1212
analysis). Also, it would bind both references to 'x' to the same
13-
module-level variable node. The second assignment would also be
14-
analyzed, and the type of 'y' marked as being inferred.
13+
module-level variable (Var) node. The second assignment would also
14+
be analyzed, and the type of 'y' marked as being inferred.
1515
1616
Semantic analysis is the first analysis pass after parsing, and it is
1717
subdivided into three passes:
1818
19-
* FirstPass looks up externally visible names defined in a module but
20-
ignores imports and local definitions. It helps enable (some)
21-
cyclic references between modules, such as module 'a' that imports
22-
module 'b' and used names defined in b *and* vice versa. The first
23-
pass can be performed before dependent modules have been processed.
19+
* SemanticAnalyzerPass1 looks up externally visible names defined in a
20+
module but ignores imports and local definitions. It helps enable
21+
(some) cyclic references between modules, such as module 'a' that
22+
imports module 'b' and used names defined in b *and* vice versa. The
23+
first pass can be performed before dependent modules have been
24+
processed.
2425
25-
* SemanticAnalyzer is the second pass. It does the bulk of the work.
26+
* SemanticAnalyzerPass2 is the second pass. It does the bulk of the work.
2627
It assumes that dependent modules have been semantically analyzed,
2728
up to the second pass, unless there is a import cycle.
2829
29-
* ThirdPass checks that type argument counts are valid; for example,
30-
it will reject Dict[int]. We don't do this in the second pass,
31-
since we infer the type argument counts of classes during this
32-
pass, and it is possible to refer to classes defined later in a
30+
* SemanticAnalyzerPass3 checks that type argument counts are valid;
31+
for example, it will reject Dict[int]. We don't do this in the
32+
second pass, since we infer the type argument counts of classes during
33+
this pass, and it is possible to refer to classes defined later in a
3334
file, which would not have the type argument count set yet. This
3435
pass also recomputes the method resolution order of each class, in
3536
case one of its bases belongs to a module involved in an import
@@ -183,7 +184,7 @@
183184
}
184185

185186

186-
class SemanticAnalyzer(NodeVisitor[None]):
187+
class SemanticAnalyzerPass2(NodeVisitor[None]):
187188
"""Semantically analyze parsed mypy files.
188189
189190
The analyzer binds names and does various consistency checks for a
@@ -1078,7 +1079,7 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
10781079

10791080
# Calculate the MRO. It might be incomplete at this point if
10801081
# the bases of defn include classes imported from other
1081-
# modules in an import loop. We'll recompute it in ThirdPass.
1082+
# modules in an import loop. We'll recompute it in SemanticAnalyzerPass3.
10821083
if not self.verify_base_classes(defn):
10831084
# Give it an MRO consisting of just the class itself and object.
10841085
defn.info.mro = [defn.info, self.object_type().type]
@@ -3795,13 +3796,13 @@ def accept(self, node: Node) -> None:
37953796
report_internal_error(err, self.errors.file, node.line, self.errors, self.options)
37963797

37973798

3798-
class FirstPass(NodeVisitor[None]):
3799+
class SemanticAnalyzerPass1(NodeVisitor[None]):
37993800
"""First phase of semantic analysis.
38003801
38013802
See docstring of 'analyze()' below for a description of what this does.
38023803
"""
38033804

3804-
def __init__(self, sem: SemanticAnalyzer) -> None:
3805+
def __init__(self, sem: SemanticAnalyzerPass2) -> None:
38053806
self.sem = sem
38063807

38073808
def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -> None:
@@ -4064,15 +4065,15 @@ def kind_by_scope(self) -> int:
40644065
assert False, "Couldn't determine scope"
40654066

40664067

4067-
class ThirdPass(TraverserVisitor):
4068+
class SemanticAnalyzerPass3(TraverserVisitor):
40684069
"""The third and final pass of semantic analysis.
40694070
40704071
Check type argument counts and values of generic types, and perform some
40714072
straightforward type inference.
40724073
"""
40734074

40744075
def __init__(self, modules: Dict[str, MypyFile], errors: Errors,
4075-
sem: SemanticAnalyzer) -> None:
4076+
sem: SemanticAnalyzerPass2) -> None:
40764077
self.modules = modules
40774078
self.errors = errors
40784079
self.sem = sem
@@ -4833,15 +4834,16 @@ def visit_forwardref_type(self, t: ForwardRef) -> Type:
48334834
"""This visitor method tracks situations like this:
48344835
48354836
x: A # This type is not yet known and therefore wrapped in ForwardRef,
4836-
# its content is updated in ThirdPass, now we need to unwrap this type.
4837+
# its content is updated in SemanticAnalyzerPass3, now we need to unwrap
4838+
# this type.
48374839
A = NewType('A', int)
48384840
"""
48394841
return t.link.accept(self)
48404842

48414843
def visit_instance(self, t: Instance, from_fallback: bool = False) -> Type:
48424844
"""This visitor method tracks situations like this:
48434845
4844-
x: A # When analyzing this type we will get an Instance from FirstPass.
4846+
x: A # When analyzing this type we will get an Instance from SemanticAnalyzerPass1.
48454847
# Now we need to update this to actual analyzed TupleType.
48464848
class A(NamedTuple):
48474849
attr: str

0 commit comments

Comments
 (0)