MCPcopy Index your code
hub / github.com/python/mypy / transform_name_expr

Function transform_name_expr

mypyc/irbuild/expression.py:146–234  ·  view source on GitHub ↗
(builder: IRBuilder, expr: NameExpr)

Source from the content-addressed store, hash-verified

144
145
146def 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 ),

Callers 1

visit_name_exprMethod · 0.90

Calls 15

RaiseStandardErrorClass · 0.90
AnyTypeClass · 0.90
is_none_rprimitiveFunction · 0.90
isinstanceFunction · 0.85
transform_math_literalFunction · 0.85
load_type_varMethod · 0.80
emit_load_finalMethod · 0.80
node_typeMethod · 0.80
get_assignment_targetMethod · 0.80
addMethod · 0.45
noneMethod · 0.45
load_builtinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…