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

Method analyze_ref_expr

mypy/checkexpr.py:354–397  ·  view source on GitHub ↗
(self, e: RefExpr, lvalue: bool = False)

Source from the content-addressed store, hash-verified

352 return narrowed
353
354 def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
355 result: Type | None = None
356 node = e.node
357
358 if isinstance(e, NameExpr) and e.is_special_form:
359 # A special form definition, nothing to check here.
360 return AnyType(TypeOfAny.special_form)
361
362 if isinstance(node, Var):
363 # Variable reference.
364 result = self.analyze_var_ref(node, e)
365 if isinstance(result, PartialType):
366 result = self.chk.handle_partial_var_type(result, lvalue, node, e)
367 elif isinstance(node, Decorator):
368 result = self.analyze_var_ref(node.var, e)
369 elif isinstance(node, OverloadedFuncDef):
370 if node.type is None:
371 if self.chk.in_checked_function() and node.items:
372 self.chk.handle_cannot_determine_type(node.name, e)
373 result = AnyType(TypeOfAny.from_error)
374 else:
375 result = node.type
376 elif isinstance(node, (FuncDef, TypeInfo, TypeAlias, MypyFile, TypeVarLikeExpr)):
377 result = self.analyze_static_reference(node, e, e.is_alias_rvalue or lvalue)
378 else:
379 if isinstance(node, PlaceholderNode):
380 assert False, f"PlaceholderNode {node.fullname!r} leaked to checker"
381 # Unknown reference; use any type implicitly to avoid
382 # generating extra type errors.
383 result = AnyType(TypeOfAny.from_error)
384 if isinstance(node, TypeInfo):
385 if isinstance(result, CallableType) and isinstance( # type: ignore[misc]
386 result.ret_type, Instance
387 ):
388 # We need to set correct line and column
389 # TODO: always do this in type_object_type by passing the original context
390 result.ret_type.line = e.line
391 result.ret_type.column = e.column
392 if is_type_type_context(self.type_context[-1]):
393 # This is the type in a type[] expression, so substitute type
394 # variables with Any.
395 result = erasetype.erase_typevars(result)
396 assert result is not None
397 return result
398
399 def analyze_static_reference(
400 self,

Callers 4

visit_name_exprMethod · 0.95
check_lvalueMethod · 0.45

Calls 8

analyze_var_refMethod · 0.95
AnyTypeClass · 0.90
isinstanceFunction · 0.85
is_type_type_contextFunction · 0.85
in_checked_functionMethod · 0.45

Tested by

no test coverage detected