/
node_protocols.py
104 lines (70 loc) · 2.54 KB
/
node_protocols.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from __future__ import annotations
from abc import abstractmethod
from typing import Any
from typing import Callable
from typing import Protocol
from typing import runtime_checkable
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from _pytask.tree_util import PyTree
from pathlib import Path
from _pytask.mark import Mark
__all__ = ["PNode", "PPathNode", "PTask", "PTaskWithPath"]
@runtime_checkable
class PNode(Protocol):
"""Protocol for nodes."""
name: str
@property
def signature(self) -> str:
"""Return the signature of the node."""
@abstractmethod
def state(self) -> str | None:
"""Return the state of the node.
The state can be something like a hash or a last modified timestamp. If the node
does not exist, you can also return ``None``.
"""
def load(self, is_product: bool) -> Any:
"""Return the value of the node that will be injected into the task.
Parameters
----------
is_product
Indicates whether the node is loaded as a dependency or as a product. It can
be used to return a different value when the node is loaded with a product
annotation. Then, we usually want to insert the node itself to allow the
user calling :meth:`PNode.load`.
"""
def save(self, value: Any) -> Any:
"""Save the value that was returned from a task."""
@runtime_checkable
class PPathNode(PNode, Protocol):
"""Nodes with paths.
Nodes with paths receive special handling when it comes to printing their names.
"""
path: Path
@runtime_checkable
class PTask(Protocol):
"""Protocol for nodes."""
name: str
depends_on: dict[str, PyTree[PNode]]
produces: dict[str, PyTree[PNode]]
function: Callable[..., Any]
markers: list[Mark]
report_sections: list[tuple[str, str, str]]
attributes: dict[Any, Any]
@property
def signature(self) -> str:
"""Return the signature of the node."""
@abstractmethod
def state(self) -> str | None:
"""Return the state of the node.
The state can be something like a hash or a last modified timestamp. If the node
does not exist, you can also return ``None``.
"""
def execute(self, **kwargs: Any) -> Any:
"""Return the value of the node that will be injected into the task."""
@runtime_checkable
class PTaskWithPath(PTask, Protocol):
"""Tasks with paths.
Tasks with paths receive special handling when it comes to printing their names.
"""
path: Path