Skip to content
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 @@ -26,6 +26,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
- {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.
- {pull}`407` make `.from_annot` an optional feature of nodes.

## 0.3.2 - 2023-06-07

Expand Down
8 changes: 7 additions & 1 deletion src/_pytask/collect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,5 +601,11 @@ def _evolve_instance(x: Any, instance_from_annot: Node | None) -> Any:
if not instance_from_annot:
return x

instance_from_annot.from_annot(x)
if not hasattr(instance_from_annot, "from_annot"):
raise AttributeError(
f"The node {instance_from_annot!r} does not define '.from_annot' which is "
f"necessary to complete the node with the value {x!r}."
)

instance_from_annot.from_annot(x) # type: ignore[attr-defined]
return instance_from_annot
15 changes: 0 additions & 15 deletions src/_pytask/node_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,6 @@ def save(self, value: Any) -> Any:
"""Save the value that was returned from a task."""
...

def from_annot(self, value: Any) -> Any:
"""Complete the node by setting the value from an default argument.

Use it, if you want to add information on how a node handles an argument while
keeping the type of the value unrelated to pytask.

.. codeblock: python

def task_example(value: Annotated[Any, PythonNode(hash=True)], produces):
...


"""
...


@runtime_checkable
class PPathNode(Node, Protocol):
Expand Down
25 changes: 23 additions & 2 deletions src/_pytask/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ class PathNode(PPathNode):
"""The path to the file."""

def from_annot(self, value: Path) -> None:
"""Set path and if other attributes are not set, set sensible defaults."""
"""Set path and if other attributes are not set, set sensible defaults.

Use it, if you want to control the name of the node.

.. codeblock: python

def task_example(value: Annotated[Any, PathNode(name="value")]):
...


"""
if not isinstance(value, Path):
raise TypeError("'value' must be a 'pathlib.Path'.")
if not self.name:
Expand Down Expand Up @@ -176,7 +186,18 @@ def save(self, value: Any) -> None:
self.value = value

def from_annot(self, value: Any) -> None:
"""Set the value from a function annotation."""
"""Set the value from a function annotation.

Use it, if you want to add information on how a node handles an argument while
keeping the type of the value unrelated to pytask. For example, the node could
be hashed.

.. codeblock: python

def task_example(value: Annotated[Any, PythonNode(hash=True)]):
...

"""
self.value = value

def state(self) -> str | None:
Expand Down