(builder: IRBuilder, expr: NameExpr)
| 144 | |
| 145 | |
| 146 | def transform_name_expr(builder: IRBuilder, expr: NameExpr) -> Value: |
| 147 | if isinstance(expr.node, TypeVarLikeExpr) and expr.node.is_new_style: |
| 148 | # Reference to Python 3.12 implicit TypeVar/TupleVarTuple/... object. |
| 149 | # These are stored in C statics and not visible in Python namespaces. |
| 150 | return builder.load_type_var(expr.node.name, expr.node.line) |
| 151 | if expr.node is None: |
| 152 | builder.add( |
| 153 | RaiseStandardError( |
| 154 | RaiseStandardError.NAME_ERROR, f'name "{expr.name}" is not defined', expr.line |
| 155 | ) |
| 156 | ) |
| 157 | return builder.none(expr.line) |
| 158 | fullname = expr.node.fullname |
| 159 | if builtin := builder.load_builtin(fullname, expr.line): |
| 160 | return builtin |
| 161 | # special cases |
| 162 | if fullname == "builtins.None": |
| 163 | return builder.none(expr.line) |
| 164 | if fullname == "builtins.True": |
| 165 | return builder.true(expr.line) |
| 166 | if fullname == "builtins.False": |
| 167 | return builder.false(expr.line) |
| 168 | if fullname in ("typing.TYPE_CHECKING", "typing_extensions.TYPE_CHECKING"): |
| 169 | return builder.false(expr.line) |
| 170 | |
| 171 | math_literal = transform_math_literal(builder, fullname, expr.line) |
| 172 | if math_literal is not None: |
| 173 | return math_literal |
| 174 | |
| 175 | if isinstance(expr.node, Var) and expr.node.is_final: |
| 176 | final_type = builder.types.get(expr) or expr.node.type |
| 177 | if final_type is None: |
| 178 | final_type = AnyType(TypeOfAny.special_form) |
| 179 | value = builder.emit_load_final( |
| 180 | expr.node, fullname, expr.name, builder.is_native_ref_expr(expr), final_type, expr.line |
| 181 | ) |
| 182 | if value is not None: |
| 183 | return value |
| 184 | |
| 185 | if isinstance(expr.node, MypyFile) and expr.node.fullname in builder.imports: |
| 186 | return builder.load_module(expr.node.fullname) |
| 187 | |
| 188 | # If the expression is locally defined, then read the result from the corresponding |
| 189 | # assignment target and return it. Otherwise if the expression is a global, load it from |
| 190 | # the globals dictionary. |
| 191 | # Except for imports, that currently always happens in the global namespace. |
| 192 | if expr.kind == LDEF and not (isinstance(expr.node, Var) and expr.node.is_suppressed_import): |
| 193 | # Try to detect and error when we hit the irritating mypy bug |
| 194 | # where a local variable is cast to None. (#5423) |
| 195 | if ( |
| 196 | isinstance(expr.node, Var) |
| 197 | and is_none_rprimitive(builder.node_type(expr)) |
| 198 | and expr.node.is_inferred |
| 199 | ): |
| 200 | builder.error( |
| 201 | 'Local variable "{}" has inferred type None; add an annotation'.format( |
| 202 | expr.node.name |
| 203 | ), |
no test coverage detected
searching dependent graphs…