Hi, I’m trying to adopt Hamilton to replace parts ...
# hamilton-help
y
Hi, I’m trying to adopt Hamilton to replace parts of an existing framework and have a couple of questions: 1. Let’s say some nodes are decorated with
@tag(node_type="output", table_name="my_table")
. Is there a way to make sure that any node tagged with
node_type="output"
must also have a
table_name
, other than creating a decorator by wrapping
tag
? 2. How do you recommend checking all
node_type="output"
nodes have a required output type
OutputType
? I’m thinking about using the
type
class variable on the nodes. Thank you!
s
When would you want this check to run? As part of CI? Before every execution?
y
Before every execution
e
Hey! So you can run this check yourself by implementing this hook, and passing an instance it into the with_adapters in the driver builder. You can do the node-level hook — this way you can check the tags. Conversely, a custom separate decorator will also do the trick.
👍 1
y
Thanks for the advice!
e
s
Sorry @Yijun Tang I was AFK. If you need an example implementation let us know. The validator should really just implement the node validator and check the tags and validate what you want. Similarly with the output type.
y
The
StaticValidator
looks like exactly what I’m looking for! Would appreciate some simple examples, especially on how to traverse the nodes in a
graph: HamiltonGraph
, and what attributes are available on a
node: HamiltonNode
for validation purposes. 🙏
s
@Yijun Tang here you go:
Copy code
from typing import Optional
from hamilton.lifecycle import api
from hamilton.graph_types import HamiltonNode

class MyTagValidator(api.StaticValidator):

    def run_to_validate_node(
            self, *, node: HamiltonNode, **future_kwargs
    ) -> tuple[bool, Optional[str]]:
        print(node.tags)
        if node.tags.get("node_type", "") == "output":
            table_name = node.tags.get("table_name")
            if not table_name:  # None or empty
                error_msg = f"Node {node.tags['module']}.{node.name} is an output node, but does not have a table_name tag."
                return False, error_msg
        return True, None

if __name__ == '__main__':
    from hamilton import driver
    import good_module
    tag_validator = MyTagValidator()
    dr = (
        driver.Builder()
        .with_modules(good_module)
        .with_adapters(tag_validator)
        .build()
    )
    print(dr.execute([good_module.foo]))

    import bad_module
    # this should error
    dr = (
        driver.Builder()
        .with_modules(bad_module)
        .with_adapters(tag_validator)
        .build()
    )
# good_module.py
Copy code
from hamilton.function_modifiers import tag

@tag(node_type="output", table_name="my_table")
def foo() -> str:
    return "Hello, world!"
# bad_module.py
Copy code
from hamilton.function_modifiers import tag

@tag(node_type="output")
def foo() -> str:
    return "Hello, world!"
y
Thanks for such a fast turnaround! So basically
run_to_validate_node
will be applied to all nodes in a DAG?
s
> Thanks for such a fast turnaround! So basically
run_to_validate_node
will be applied to all nodes in a DAG? correct. after
.build()
is called.
👍 1