|
10 | 10 | defines a new variable, the type of which is to be inferred (in a
|
11 | 11 | later pass; type inference or type checking is not part of semantic
|
12 | 12 | 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. |
15 | 15 |
|
16 | 16 | Semantic analysis is the first analysis pass after parsing, and it is
|
17 | 17 | subdivided into three passes:
|
18 | 18 |
|
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. |
24 | 25 |
|
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. |
26 | 27 | It assumes that dependent modules have been semantically analyzed,
|
27 | 28 | up to the second pass, unless there is a import cycle.
|
28 | 29 |
|
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 |
33 | 34 | file, which would not have the type argument count set yet. This
|
34 | 35 | pass also recomputes the method resolution order of each class, in
|
35 | 36 | case one of its bases belongs to a module involved in an import
|
|
183 | 184 | }
|
184 | 185 |
|
185 | 186 |
|
186 |
| -class SemanticAnalyzer(NodeVisitor[None]): |
| 187 | +class SemanticAnalyzerPass2(NodeVisitor[None]): |
187 | 188 | """Semantically analyze parsed mypy files.
|
188 | 189 |
|
189 | 190 | The analyzer binds names and does various consistency checks for a
|
@@ -1078,7 +1079,7 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
|
1078 | 1079 |
|
1079 | 1080 | # Calculate the MRO. It might be incomplete at this point if
|
1080 | 1081 | # 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. |
1082 | 1083 | if not self.verify_base_classes(defn):
|
1083 | 1084 | # Give it an MRO consisting of just the class itself and object.
|
1084 | 1085 | defn.info.mro = [defn.info, self.object_type().type]
|
@@ -3795,13 +3796,13 @@ def accept(self, node: Node) -> None:
|
3795 | 3796 | report_internal_error(err, self.errors.file, node.line, self.errors, self.options)
|
3796 | 3797 |
|
3797 | 3798 |
|
3798 |
| -class FirstPass(NodeVisitor[None]): |
| 3799 | +class SemanticAnalyzerPass1(NodeVisitor[None]): |
3799 | 3800 | """First phase of semantic analysis.
|
3800 | 3801 |
|
3801 | 3802 | See docstring of 'analyze()' below for a description of what this does.
|
3802 | 3803 | """
|
3803 | 3804 |
|
3804 |
| - def __init__(self, sem: SemanticAnalyzer) -> None: |
| 3805 | + def __init__(self, sem: SemanticAnalyzerPass2) -> None: |
3805 | 3806 | self.sem = sem
|
3806 | 3807 |
|
3807 | 3808 | def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -> None:
|
@@ -4064,15 +4065,15 @@ def kind_by_scope(self) -> int:
|
4064 | 4065 | assert False, "Couldn't determine scope"
|
4065 | 4066 |
|
4066 | 4067 |
|
4067 |
| -class ThirdPass(TraverserVisitor): |
| 4068 | +class SemanticAnalyzerPass3(TraverserVisitor): |
4068 | 4069 | """The third and final pass of semantic analysis.
|
4069 | 4070 |
|
4070 | 4071 | Check type argument counts and values of generic types, and perform some
|
4071 | 4072 | straightforward type inference.
|
4072 | 4073 | """
|
4073 | 4074 |
|
4074 | 4075 | def __init__(self, modules: Dict[str, MypyFile], errors: Errors,
|
4075 |
| - sem: SemanticAnalyzer) -> None: |
| 4076 | + sem: SemanticAnalyzerPass2) -> None: |
4076 | 4077 | self.modules = modules
|
4077 | 4078 | self.errors = errors
|
4078 | 4079 | self.sem = sem
|
@@ -4833,15 +4834,16 @@ def visit_forwardref_type(self, t: ForwardRef) -> Type:
|
4833 | 4834 | """This visitor method tracks situations like this:
|
4834 | 4835 |
|
4835 | 4836 | 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. |
4837 | 4839 | A = NewType('A', int)
|
4838 | 4840 | """
|
4839 | 4841 | return t.link.accept(self)
|
4840 | 4842 |
|
4841 | 4843 | def visit_instance(self, t: Instance, from_fallback: bool = False) -> Type:
|
4842 | 4844 | """This visitor method tracks situations like this:
|
4843 | 4845 |
|
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. |
4845 | 4847 | # Now we need to update this to actual analyzed TupleType.
|
4846 | 4848 | class A(NamedTuple):
|
4847 | 4849 | attr: str
|
|
0 commit comments