Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test optional dependencies #790

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading