Skip to content

Commit

Permalink
test optional dependencies (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ authored Apr 26, 2024
1 parent 591f7ff commit d2170df
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/integration/test_node_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,20 @@ def test_AddNodeAttributes_legacy(proj_path):
assert add_numbers_b.state.loaded
assert add_nodes.c == 7
assert add_nodes.state.loaded


def test_OptionalDeps(proj_path):
with zntrack.Project() as proj:
add_numbers = zntrack.examples.AddNumbers(a=1, b=2)
add_none = zntrack.examples.OptionalDeps()
add_value = zntrack.examples.OptionalDeps(value=add_numbers.c)

proj.run()

add_numbers.load()
add_none.load()
add_value.load()

assert add_numbers.c == 3
assert add_none.result == 0.0
assert add_value.result == 3.0
11 changes: 11 additions & 0 deletions zntrack/examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,14 @@ def run(self) -> None:
assert self.state.restarted
if self.state.run_count < self.raise_exception_until:
raise ValueError("This is a test exception, simulating killing the Node.")


class OptionalDeps(zntrack.Node):
"""Node with optional dependencies."""

value: float = zntrack.deps(None)
result: float = zntrack.outs()

def run(self) -> None:
"""Run the node."""
self.result = self.value or 0.0

0 comments on commit d2170df

Please sign in to comment.