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
55 changes: 42 additions & 13 deletions flowrep/models/parsers/object_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,57 @@ def get_scope(func: FunctionType) -> ScopeProxy:
return ScopeProxy(inspect.getmodule(func).__dict__ | vars(builtins))


def resolve_attribute_to_object(attribute: str, scope: ScopeProxy | object) -> object:
"""
Resolve a dot-separated attribute string to the actual object it references in the
given scope. For example, if attribute is "os.path.join", this function will
return the actual join function from the os.path module.

Args:
attribute: A dot-separated string representing the attribute to resolve.
scope: The scope in which to resolve the attribute. This can be a ScopeProxy
or any object that supports attribute access.

Returns:
The object that the attribute resolves to in the given scope.
"""
obj = None
try:
for attr in attribute.split("."):
obj = getattr(obj or scope, attr)
return obj
except AttributeError as e:
raise ValueError(f"Could not find attribute '{attr}' of {attribute}") from e
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message uses 'attr' which only contains the last attribute from the loop iteration, not necessarily the one that failed. This creates a misleading error message. For example, if "math.cos.nonexistent" fails at "cos", the error will show "Could not find attribute 'nonexistent' of math.cos.nonexistent". Consider capturing which specific attribute lookup failed or restructuring the error to be more accurate about what part of the chain caused the issue.

Suggested change
raise ValueError(f"Could not find attribute '{attr}' of {attribute}") from e
raise ValueError(f"Could not resolve attribute chain '{attribute}'") from e

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new 'resolve_attribute_to_object' function lacks test coverage. While existing tests for 'resolve_symbol_to_object' indirectly exercise this function, there are no direct tests verifying its behavior with string inputs like "math.cos". Consider adding tests for this new public function to ensure it works correctly when called directly, which is the purpose mentioned in the PR description.

Suggested change
raise ValueError(f"Could not find attribute '{attr}' of {attribute}") from e
raise ValueError(f"Could not resolve attribute chain '{attribute}'") from e

Copilot uses AI. Check for mistakes.


def resolve_symbol_to_object(
node: ast.expr, # Expecting a Name or Attribute here, and will otherwise TypeError
scope: ScopeProxy | object,
_chain: list[str] | None = None,
) -> object:
""" """
"""
Recursively resolve a symbol in the form of an ast.Name or ast.Attribute to the
actual object it references in the given scope. The _chain parameter is used
internally to keep track of the attribute chain being resolved, and should not
be provided by the caller.

Args:
node: An ast.expr representing the symbol to resolve. Expected to be an
ast.Name or ast.Attribute.
scope: The scope in which to resolve the symbol. This can be a ScopeProxy
or any object that supports attribute access.

Returns:
The object that the symbol resolves to in the given scope.
"""
_chain = _chain or []
error_suffix = f" while attempting to resolve the symbol chain '{'.'.join(_chain)}'"
if isinstance(node, ast.Name):
attr = node.id
try:
obj = getattr(scope, attr)
for attr in _chain:
obj = getattr(obj, attr)
return obj
except AttributeError as e:
raise ValueError(f"Could not find attribute '{attr}' {error_suffix}") from e
return resolve_attribute_to_object(".".join([node.id] + _chain), scope)
elif isinstance(node, ast.Attribute):
return resolve_symbol_to_object(node.value, scope, [node.attr] + _chain)
else:
raise TypeError(
f"Cannot resolve symbol {node} {error_suffix}. "
f"Expected an ast.Name or chain of ast.Attribute and ast.Name, but got "
f"{node}."
f"Cannot resolve symbol {node} while building the symbol chain "
f"'{'.'.join(_chain)}'. Expected an ast.Name or chain of ast.Attribute "
f"and ast.Name, but got {node}."
)
9 changes: 9 additions & 0 deletions tests/unit/models/parsers/test_object_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,12 @@ def test_unrecognized_node_raises(self):
node = ast.Constant(value=42)
with self.assertRaises(TypeError):
object_scope.resolve_symbol_to_object(node, scope)

def test_resolve_attribute_to_object(self):
scope = object_scope.ScopeProxy({"ast": ast})
f = object_scope.resolve_attribute_to_object("ast.literal_eval", scope)
self.assertIs(f, ast.literal_eval)


if __name__ == "__main__":
unittest.main()
Loading