flowrep.parsers.object_scope module

class flowrep.parsers.object_scope.ScopeProxy(d: MutableMapping[str, object] | None = None, allow_overwrite: bool = False)[source]

Bases: MutableMapping[str, object]

A mutable mapping to connect symbols to python objects.

By default, does not allow re-registration of existing symbols to new values.

fork() ScopeProxy[source]

Create a shallow copy of this scope.

Modifications to the fork (e.g. registering new imports) do not affect the parent. Used when walking conditional branches so that branch-local imports don’t leak into sibling branches.

register(name: str, obj: object) None[source]

Add a name → object binding to this scope.

Used by the parser when encountering import statements inside function bodies. The binding is visible to all subsequent symbol resolutions within this scope (or any scope that shares the same backing namespace).

flowrep.parsers.object_scope.get_scope(func: Callable[[...], Any] | type[Any]) ScopeProxy[source]
flowrep.parsers.object_scope.resolve_attribute_to_object(attribute: str, scope: ScopeProxy | object) object[source]

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.

Parameters:
  • 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.

flowrep.parsers.object_scope.resolve_symbol_to_object(node: expr, scope: ScopeProxy | object, _chain: list[str] | None = None) object[source]

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.

Parameters:
  • 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.