|
4 | 4 |
|
5 | 5 | import ast |
6 | 6 | import os |
| 7 | +from types import MethodType |
7 | 8 |
|
8 | 9 | from jaclang.compiler.absyntree import AstNode |
9 | 10 | from jaclang.compiler.passes import Pass |
10 | 11 | from jaclang.compiler.passes.main.fuse_typeinfo_pass import ( |
11 | 12 | FuseTypeInfoPass, |
12 | 13 | ) |
| 14 | +from jaclang.utils.helpers import pascal_to_snake |
13 | 15 |
|
14 | 16 | import mypy.build as myb |
15 | 17 | import mypy.checkexpr as mycke |
16 | 18 | import mypy.errors as mye |
17 | 19 | import mypy.fastparse as myfp |
| 20 | +import mypy.nodes as mypy_nodes |
18 | 21 | from mypy.build import BuildSource |
19 | 22 | from mypy.build import BuildSourceSet |
20 | 23 | from mypy.build import FileSystemCache |
|
29 | 32 | from mypy.semanal_main import semantic_analysis_for_scc |
30 | 33 |
|
31 | 34 |
|
| 35 | +# All the expression nodes of mypy. |
| 36 | +EXPRESSION_NODES = ( |
| 37 | + mypy_nodes.AssertTypeExpr, |
| 38 | + mypy_nodes.AssignmentExpr, |
| 39 | + mypy_nodes.AwaitExpr, |
| 40 | + mypy_nodes.BytesExpr, |
| 41 | + mypy_nodes.CallExpr, |
| 42 | + mypy_nodes.CastExpr, |
| 43 | + mypy_nodes.ComparisonExpr, |
| 44 | + mypy_nodes.ComplexExpr, |
| 45 | + mypy_nodes.ConditionalExpr, |
| 46 | + mypy_nodes.DictionaryComprehension, |
| 47 | + mypy_nodes.DictExpr, |
| 48 | + mypy_nodes.EllipsisExpr, |
| 49 | + mypy_nodes.EnumCallExpr, |
| 50 | + mypy_nodes.Expression, |
| 51 | + mypy_nodes.FloatExpr, |
| 52 | + mypy_nodes.GeneratorExpr, |
| 53 | + mypy_nodes.IndexExpr, |
| 54 | + mypy_nodes.IntExpr, |
| 55 | + mypy_nodes.LambdaExpr, |
| 56 | + mypy_nodes.ListComprehension, |
| 57 | + mypy_nodes.ListExpr, |
| 58 | + mypy_nodes.MemberExpr, |
| 59 | + mypy_nodes.NamedTupleExpr, |
| 60 | + mypy_nodes.NameExpr, |
| 61 | + mypy_nodes.NewTypeExpr, |
| 62 | + mypy_nodes.OpExpr, |
| 63 | + mypy_nodes.ParamSpecExpr, |
| 64 | + mypy_nodes.PromoteExpr, |
| 65 | + mypy_nodes.RefExpr, |
| 66 | + mypy_nodes.RevealExpr, |
| 67 | + mypy_nodes.SetComprehension, |
| 68 | + mypy_nodes.SetExpr, |
| 69 | + mypy_nodes.SliceExpr, |
| 70 | + mypy_nodes.StarExpr, |
| 71 | + mypy_nodes.StrExpr, |
| 72 | + mypy_nodes.SuperExpr, |
| 73 | + mypy_nodes.TupleExpr, |
| 74 | + mypy_nodes.TypeAliasExpr, |
| 75 | + mypy_nodes.TypedDictExpr, |
| 76 | + mypy_nodes.TypeVarExpr, |
| 77 | + mypy_nodes.TypeVarTupleExpr, |
| 78 | + mypy_nodes.UnaryExpr, |
| 79 | + mypy_nodes.YieldExpr, |
| 80 | + mypy_nodes.YieldFromExpr, |
| 81 | +) |
| 82 | + |
| 83 | + |
32 | 84 | mypy_to_jac_node_map: dict[ |
33 | 85 | tuple[int, int | None, int | None, int | None], list[AstNode] |
34 | 86 | ] = {} |
@@ -87,63 +139,45 @@ def __init__( |
87 | 139 | """Override to mypy expression checker for direct AST pass through.""" |
88 | 140 | super().__init__(tc, msg, plugin, per_line_checking_time_ns) |
89 | 141 |
|
90 | | - def visit_list_expr(self, e: mycke.ListExpr) -> mycke.Type: |
91 | | - """Type check a list expression [...].""" |
92 | | - out = super().visit_list_expr(e) |
93 | | - FuseTypeInfoPass.node_type_hash[e] = out |
94 | | - return out |
95 | | - |
96 | | - def visit_set_expr(self, e: mycke.SetExpr) -> mycke.Type: |
97 | | - """Type check a set expression {...}.""" |
98 | | - out = super().visit_set_expr(e) |
99 | | - FuseTypeInfoPass.node_type_hash[e] = out |
100 | | - return out |
101 | | - |
102 | | - def visit_tuple_expr(self, e: myfp.TupleExpr) -> myb.Type: |
103 | | - """Type check a tuple expression (...).""" |
104 | | - out = super().visit_tuple_expr(e) |
105 | | - FuseTypeInfoPass.node_type_hash[e] = out |
106 | | - return out |
107 | | - |
108 | | - def visit_dict_expr(self, e: myfp.DictExpr) -> myb.Type: |
109 | | - """Type check a dictionary expression {...}.""" |
110 | | - out = super().visit_dict_expr(e) |
111 | | - FuseTypeInfoPass.node_type_hash[e] = out |
112 | | - return out |
113 | | - |
114 | | - def visit_list_comprehension(self, e: myfp.ListComprehension) -> myb.Type: |
115 | | - """Type check a list comprehension.""" |
116 | | - out = super().visit_list_comprehension(e) |
117 | | - FuseTypeInfoPass.node_type_hash[e] = out |
118 | | - return out |
119 | | - |
120 | | - def visit_set_comprehension(self, e: myfp.SetComprehension) -> myb.Type: |
121 | | - """Type check a set comprehension.""" |
122 | | - out = super().visit_set_comprehension(e) |
123 | | - FuseTypeInfoPass.node_type_hash[e] = out |
124 | | - return out |
125 | | - |
126 | | - def visit_generator_expr(self, e: myfp.GeneratorExpr) -> myb.Type: |
127 | | - """Type check a generator expression.""" |
128 | | - out = super().visit_generator_expr(e) |
129 | | - FuseTypeInfoPass.node_type_hash[e] = out |
130 | | - return out |
131 | | - |
132 | | - def visit_dictionary_comprehension( |
133 | | - self, e: myfp.DictionaryComprehension |
134 | | - ) -> myb.Type: |
135 | | - """Type check a dict comprehension.""" |
136 | | - out = super().visit_dictionary_comprehension(e) |
137 | | - FuseTypeInfoPass.node_type_hash[e] = out |
138 | | - return out |
139 | | - |
140 | | - def visit_member_expr( |
141 | | - self, e: myfp.MemberExpr, is_lvalue: bool = False |
142 | | - ) -> myb.Type: |
143 | | - """Type check a member expr.""" |
144 | | - out = super().visit_member_expr(e, is_lvalue) |
145 | | - FuseTypeInfoPass.node_type_hash[e] = out |
146 | | - return out |
| 142 | + # For every expression there, create attach a method on this instance (self) named "enter_expr()" |
| 143 | + for expr_node in EXPRESSION_NODES: |
| 144 | + method_name = "visit_" + pascal_to_snake(expr_node.__name__) |
| 145 | + |
| 146 | + # We call the super() version of the method so ensure the parent class has the method or else continue. |
| 147 | + if not hasattr(mycke.ExpressionChecker, method_name): |
| 148 | + continue |
| 149 | + |
| 150 | + # If the method already overriden then don't override it again here. Continue. Note that the method exists |
| 151 | + # on the parent class and if it's also exists on this class and it's a different object that means it's |
| 152 | + # overrident method. |
| 153 | + if getattr(mycke.ExpressionChecker, method_name) != getattr( |
| 154 | + ExpressionChecker, method_name |
| 155 | + ): |
| 156 | + continue |
| 157 | + |
| 158 | + # Since the "closure" function bellow captures the method name inside it, we cannot use it directly as the |
| 159 | + # "method_name" variable is used inside a loop and by the time the closure close the "method_name" value, |
| 160 | + # it'll be changed by the loop, so we need another method ("make_closure") to persist the value. |
| 161 | + def make_closure(method_name: str): # noqa: ANN201 |
| 162 | + def closure( |
| 163 | + self: ExpressionChecker, |
| 164 | + e: mycke.Expression, |
| 165 | + *args, # noqa: ANN002 |
| 166 | + **kwargs, # noqa: ANN003 |
| 167 | + ) -> mycke.Type: |
| 168 | + # Ignore B023 here since we bind loop variable properly but flake8 raise a false alarm |
| 169 | + # (in some version of it), a bug in flake8 (https://github.com/PyCQA/flake8-bugbear/issues/269). |
| 170 | + out = getattr(mycke.ExpressionChecker, method_name)( # noqa: B023 |
| 171 | + self, e, *args, **kwargs |
| 172 | + ) |
| 173 | + FuseTypeInfoPass.node_type_hash[e] = out |
| 174 | + return out |
| 175 | + |
| 176 | + return closure |
| 177 | + |
| 178 | + # Attach the new "visit_expr()" method to this instance. |
| 179 | + method = make_closure(method_name) |
| 180 | + setattr(self, method_name, MethodType(method, self)) |
147 | 181 |
|
148 | 182 |
|
149 | 183 | class State(myb.State): |
|
0 commit comments