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

Method collect_nodes

mypy/inspections.py:364–404  ·  view source on GitHub ↗

Collect nodes that can be referred to by an expression. Note: it can be more than one for example in case of a union attribute.

(self, expression: RefExpr)

Source from the content-addressed store, hash-verified

362 return f"{module.path}:{node.line}:{node.column + 1}:{node.name}"
363
364 def collect_nodes(self, expression: RefExpr) -> list[FuncBase | SymbolNode]:
365 """Collect nodes that can be referred to by an expression.
366
367 Note: it can be more than one for example in case of a union attribute.
368 """
369 node: FuncBase | SymbolNode | None = expression.node
370 nodes: list[FuncBase | SymbolNode]
371 if node is None:
372 # Tricky case: instance attribute
373 if isinstance(expression, MemberExpr) and expression.kind is None:
374 base_type = self.fg_manager.manager.all_types.get(expression.expr)
375 if base_type is None:
376 return []
377
378 # Now we use the base type to figure out where the attribute is defined.
379 base_type = get_proper_type(base_type)
380 instances = get_instance_fallback(base_type)
381 nodes = []
382 for instance in instances:
383 node = find_node(expression.name, instance.type)
384 if node:
385 nodes.append(node)
386 if not nodes:
387 # Try checking class namespace if attribute is on a class object.
388 if isinstance(base_type, FunctionLike) and base_type.is_type_obj():
389 instances = get_instance_fallback(
390 fill_typevars_with_any(base_type.type_object())
391 )
392 for instance in instances:
393 node = find_node(expression.name, instance.type)
394 if node:
395 nodes.append(node)
396 else:
397 # Still no luck, give up.
398 return []
399 else:
400 return []
401 else:
402 # Easy case: a module-level definition
403 nodes = [node]
404 return nodes
405
406 def modules_for_nodes(
407 self, nodes: list[FuncBase | SymbolNode], expression: RefExpr

Callers 1

expression_defMethod · 0.95

Calls 9

get_proper_typeFunction · 0.90
fill_typevars_with_anyFunction · 0.90
isinstanceFunction · 0.85
get_instance_fallbackFunction · 0.85
find_nodeFunction · 0.85
appendMethod · 0.80
getMethod · 0.45
is_type_objMethod · 0.45
type_objectMethod · 0.45

Tested by

no test coverage detected