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 cached_property #877

Merged
merged 3 commits into from
Feb 1, 2025
Merged
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
46 changes: 46 additions & 0 deletions tests/integration/test_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import functools

import zntrack.examples


class NodeWithProperty(zntrack.Node):
params: int = zntrack.params(42)
outs: dict = zntrack.outs()

def run(self):
self.outs = {"outs": self.params}

@functools.cached_property
def value(self):
self.property_triggered = True
return self.outs


def test_property_access_graph_building(proj_path):
project = zntrack.Project()

with project:
node = NodeWithProperty()
zntrack.examples.DepsToMetrics(deps=node.value)

project.build()

assert not hasattr(node, "property_triggered")

project.repro()

assert not hasattr(node, "property_triggered")

# assert node.outs != -5

with project:
node = NodeWithProperty()
zntrack.examples.DepsToMetrics(deps=node.value)

assert not hasattr(node, "property_triggered")
project.build()
assert not hasattr(node, "property_triggered")

# sanity check
node.value # trigger property access
assert node.property_triggered is True
Loading