Implementation:1kbgz Tributary Node Decorator
| Knowledge Sources | |
|---|---|
| Domains | Lazy_Evaluation, AST_Analysis |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete decorator that converts methods into lazy nodes with AST-based automatic dependency detection, provided by the Tributary library.
Description
The @node decorator wraps a method on a LazyGraph subclass into a lazy Node instance. It uses the parseASTForMethod function to parse the method's source code into an AST, then getClassAttributesUsedInMethod walks the AST to find all self.xxx attribute accesses. These attribute names are stored as _nodes_to_bind on the resulting Node.
When LazyGraph.__init__ runs, it reads _nodes_to_bind and wires each referenced self.xxx node as an upstream dependency of the decorated method's node, using the << operator.
The decorator supports both @node and @node(dynamic=True) usage patterns via the _either_type utility decorator.
Usage
Import this decorator when building declarative lazy graphs with LazyGraph. Apply it to methods that reference self.xxx attributes (which must be defined as leaf nodes via self.node()).
Code Reference
Source Location
- Repository: Tributary
- File: tributary/lazy/decorator.py, tributary/parser/__init__.py
Signature
@_either_type
def node(meth, dynamic=False):
"""Convert a method into a lazy node.
Args:
meth: the method to convert
dynamic: whether the node should always re-evaluate
Returns:
A Node instance wrapping the method, with _nodes_to_bind set
"""
# Supporting AST functions (from tributary/parser/__init__.py):
def parseASTForMethod(meth):
"""Parse a method's source code into an AST module.
Args:
meth: a Python method
Returns:
(root_node, module) - the AST FunctionDef node and containing module
"""
def getClassAttributesUsedInMethod(root):
"""Walk an AST to find all self.xxx attribute accesses.
Args:
root: an AST node (e.g., FunctionDef)
Returns:
A set of attribute name strings referenced via self.xxx
"""
def isClassAttribute(node):
"""Check if an AST node represents a self.xxx attribute access.
Returns the attribute name string if True, else None/undefined.
"""
Import
from tributary.lazy import node
# or:
from tributary.lazy.decorator import node
from tributary.parser import parseASTForMethod, getClassAttributesUsedInMethod
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| meth | callable | Yes | The method to convert into a lazy node |
| dynamic | bool | No | Whether the node always re-evaluates (default False) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Node | A Node wrapping the method, with _nodes_to_bind set to detected attribute names |
Usage Examples
Basic @node Decoration
from tributary.lazy import LazyGraph, node
class Calculator(LazyGraph):
def __init__(self, **kwargs):
self.node(name="a", value=10)
self.node(name="b", value=20)
super().__init__()
@node
def sum(self):
# self.a and self.b are auto-detected as dependencies
return self.a + self.b
@node
def product(self):
return self.a * self.b
calc = Calculator()
print(calc.sum()) # 30
print(calc.product()) # 200
Dynamic @node
from tributary.lazy import LazyGraph, node
import random
class DynamicGraph(LazyGraph):
def __init__(self, **kwargs):
self.node(name="base", value=100)
super().__init__()
@node(dynamic=True)
def noisy_value(self):
# Always re-evaluates because dynamic=True
return self.base + random.random()
g = DynamicGraph()
print(g.noisy_value()) # 100.xxx
print(g.noisy_value()) # 100.yyy (different each time)