Skip to content

Add Driver helpers for variable lookup#1605

Open
officialasishkumar wants to merge 1 commit into
apache:mainfrom
officialasishkumar:feat/driver-variable-lookup
Open

Add Driver helpers for variable lookup#1605
officialasishkumar wants to merge 1 commit into
apache:mainfrom
officialasishkumar:feat/driver-variable-lookup

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

Adds public Driver helpers for looking up variables by name and retrieving the public HamiltonGraph representation without iterating over list_available_variables().

Changes

  • Added a cached Driver.variables mapping keyed by variable name.
  • Added Driver.get_variable(name) for direct variable lookup.
  • Added Driver.get_graph() to return the external HamiltonGraph API object.
  • Added regression coverage for variable lookup and graph access.

How I tested this

  • uv run --with pytest pytest tests/test_hamilton_driver.py::test_driver_variable_lookup tests/test_hamilton_driver.py::test_driver_get_graph_returns_hamilton_graph
  • uv run ruff format --check hamilton/driver.py tests/test_hamilton_driver.py
  • uv run ruff check hamilton/driver.py tests/test_hamilton_driver.py

Notes

Fixes #1138.

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future TODOs are captured in comments
  • Project documentation has been updated if adding/changing functionality.

@jernejfrank jernejfrank left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for tackling this. I left one thought about exposing the variables directly as a dict that might make sense to do later in another PR if it is needed. Lmk, your thoughts :)

Comment thread hamilton/driver.py
results = [Variable.from_node(n) for n in all_nodes]
return results

@functools.cached_property

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any particular reason for this to be cached? IMO, if the cache is required, would be better to have this as an internal variable explicitly instead of introducing hidden state

Comment thread hamilton/driver.py
Comment on lines +797 to +801
def variables(self) -> dict[str, Variable]:
"""Returns all variables in the graph keyed by name."""
return {
node_name: Variable.from_node(node_) for node_name, node_ in self.graph.nodes.items()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On second thought, if we are exposing get_graph, that should already give us the names of the nodes and combining it with get_variable makes the API consistent and cleaner. So maybe we remove variables for now and can always add it in case it is really desired.

Comment thread hamilton/driver.py
:return: Matching HamiltonNode.
:raises KeyError: If the variable does not exist in this Driver's graph.
"""
return self.variables[name]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could do build this straight from self.graph so it still retains the hashmap lookup speed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get nodes/variables from driver by name

2 participants