Skip to content

Remove .value from Node protocol. #408

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

Merged
merged 2 commits into from
Aug 29, 2023
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
1 change: 1 addition & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
implementing their own nodes.
- {pull}`404` allows to use function returns to define task products.
- {pull}`405` allows to match function returns to node annotations with prefix trees.
- {pull}`406` removes `.value` from `Node` protocol.

## 0.3.2 - 2023-06-07

Expand Down
2 changes: 0 additions & 2 deletions src/_pytask/node_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def state(self) -> str | None:
class Node(MetaNode, Protocol):
"""Protocol for nodes."""

value: Any

def load(self) -> Any:
"""Return the value of the node that will be injected into the task."""
...
Expand Down
17 changes: 7 additions & 10 deletions src/_pytask/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from _pytask.node_protocols import MetaNode
from _pytask.node_protocols import Node
from _pytask.node_protocols import PPathNode
from _pytask.tree_util import PyTree
from _pytask.tree_util import tree_leaves
from _pytask.tree_util import tree_structure
Expand Down Expand Up @@ -101,25 +102,21 @@ def add_report_section(self, when: str, key: str, content: str) -> None:


@define(kw_only=True)
class PathNode(Node):
class PathNode(PPathNode):
"""The class for a node which is a path."""

name: str = ""
"""Name of the node which makes it identifiable in the DAG."""
value: Path | None = None
"""Value passed to the decorator which can be requested inside the function."""

@property
def path(self) -> Path:
return self.value
path: Path | None = None
"""The path to the file."""

def from_annot(self, value: Path) -> None:
"""Set path and if other attributes are not set, set sensible defaults."""
if not isinstance(value, Path):
raise TypeError("'value' must be a 'pathlib.Path'.")
if not self.name:
self.name = value.as_posix()
self.value = value
self.path = value

@classmethod
@functools.lru_cache
Expand All @@ -131,7 +128,7 @@ def from_path(cls, path: Path) -> PathNode:
"""
if not path.is_absolute():
raise ValueError("Node must be instantiated from absolute path.")
return cls(name=path.as_posix(), value=path)
return cls(name=path.as_posix(), path=path)

def state(self) -> str | None:
"""Calculate the state of the node.
Expand All @@ -145,7 +142,7 @@ def state(self) -> str | None:

def load(self) -> Path:
"""Load the value."""
return self.value
return self.path

def save(self, value: bytes | str) -> None:
"""Save strings or bytes to file."""
Expand Down
6 changes: 2 additions & 4 deletions tests/test_collect_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,8 @@ def test_print_collected_tasks_with_nodes(capsys):
base_name="function",
path=Path("task_path.py"),
function=function,
depends_on={
"depends_on": PathNode(name="in.txt", value=Path("in.txt"))
},
produces={0: PathNode(name="out.txt", value=Path("out.txt"))},
depends_on={"depends_on": PathNode(name="in.txt", path=Path("in.txt"))},
produces={0: PathNode(name="out.txt", path=Path("out.txt"))},
)
]
}
Expand Down