Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion nominal_refactor_advisor/module_node_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ModuleNodeIndex(ParsedModuleFact):
parents: AstParentIndex = field(repr=False)
loads: Counter[str] = field(repr=False)
attribute_bases: frozenset[str]
attributes_by_attr: dict[str, tuple[ast.Attribute, ...]] = field(repr=False)
bound_name_counts: Counter[str] = field(repr=False)
binding_sites: dict[str, tuple[ast.AST, ...]] = field(repr=False)
identifier_text: str = field(repr=False)
Expand All @@ -69,6 +70,7 @@ def derive(cls, parsed_module: ParsedModule) -> ModuleNodeIndex:
parents = parsed_module.syntax_index.parent_index
loads: Counter[str] = Counter()
attribute_bases: set[str] = set()
attributes_by_attr: dict[str, list[ast.Attribute]] = {}
bound: Counter[str] = Counter()
package_heads: set[str] = set()
sites: dict[str, list[ast.AST]] = {}
Expand Down Expand Up @@ -116,8 +118,11 @@ def derive(cls, parsed_module: ParsedModule) -> ModuleNodeIndex:
else:
bound[name] += 1
sites.setdefault(name, []).append(node)
case ast.Attribute(value=ast.Name(id=name)):
case ast.Attribute(attr=attr_name, value=ast.Name(id=name)):
attribute_bases.add(name)
attributes_by_attr.setdefault(attr_name, []).append(node)
case ast.Attribute(attr=attr_name) if not isinstance(node.value, ast.Name):
attributes_by_attr.setdefault(attr_name, []).append(node)
bound.update(package_heads)
return cls(
module=parsed_module.module,
Expand All @@ -128,6 +133,9 @@ def derive(cls, parsed_module: ParsedModule) -> ModuleNodeIndex:
parents=parents,
loads=loads,
attribute_bases=frozenset(attribute_bases),
attributes_by_attr={
name: tuple(nodes) for name, nodes in attributes_by_attr.items()
},
bound_name_counts=bound,
binding_sites={name: tuple(nodes) for name, nodes in sites.items()},
identifier_text=(
Expand Down
19 changes: 11 additions & 8 deletions nominal_refactor_advisor/refactors/answered_method.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""A method of a class family and the calls it answers, each receiver typed by its binding, the declarations or the type checker."""

from __future__ import annotations
Expand Down Expand Up @@ -207,15 +207,18 @@
for parsed in snapshot.parsed_modules:
if self.name not in parsed.source:
continue
parents = parsed.fact(ModuleNodeIndex).parents
for node in ast.walk(parsed.module):
where = f"{parsed.file_path}:{getattr(node, 'lineno', 0)}"
if _dynamic_access(node, self.name):
raise ValueError(f"{where} reaches {self.name!r} by a string")
if node is self.method or not (
isinstance(node, ast.Attribute) and node.attr == self.name
):
index = parsed.fact(ModuleNodeIndex)
parents = index.parents
for call in index.calls:
if _dynamic_access(call, self.name):
raise ValueError(
f"{parsed.file_path}:{call.lineno} "
f"reaches {self.name!r} by a string"
)
for node in index.attributes_by_attr.get(self.name, ()):
if node is self.method:
continue
where = f"{parsed.file_path}:{node.lineno}"
parent = parents.get(node)
site = MethodReference(
parsed.file_path, node,
Expand Down
Loading