LocalNamePath

LocalNamePath#

class treescope.canonical_aliases.LocalNamePath[source]#

Bases: object

Expected path where we can find a particular object in a local scope.

The “local scope” can be any dictionary of values with string keys, but it is usually either the locals() or globals() for a particular scope (or the union of these). A local name path is only valid relative to a particular scope that was used to create it.

Variables:
  • local_name (str) – Name of the variable in the local scope that we are retrieving.

  • attribute_path (tuple[str, ...]) – Sequence of attributes identifying this object, separated by dots (“.”). For instance, if this is [“foo”, “bar”] then we expect to find the object at “{local_name}.foo.bar”.

Methods

__init__(local_name, attribute_path)

retrieve(local_scope[, forgiving])

Retrieves the object at this path.

Attributes

local_name

attribute_path

retrieve(local_scope: dict[str, Any], forgiving: bool = False) Any[source]#

Retrieves the object at this path.

Parameters:
  • local_scope – The scope in which we should retrieve this value.

  • forgiving – If True, return None on failure instead of raising an error.

Returns:

The retrieved object, or None if it wasn’t found and forgiving was True.

Raises:

KeyError, AttributeError – If the object wasn’t found and forgiving is False.