|
3 | 3 | from typing import TYPE_CHECKING |
4 | 4 | from typing import Any |
5 | 5 | from typing import Protocol |
| 6 | +from typing import TypeAlias |
6 | 7 | from typing import runtime_checkable |
7 | 8 |
|
| 9 | +from _pytask.tree_util import PyTree |
| 10 | + |
8 | 11 | if TYPE_CHECKING: |
9 | 12 | from collections.abc import Callable |
10 | 13 | from pathlib import Path |
11 | 14 |
|
12 | 15 | from _pytask.mark import Mark |
13 | | - from _pytask.tree_util import PyTree |
14 | 16 | from _pytask.typing import NodePath |
15 | 17 |
|
16 | 18 |
|
17 | | -__all__ = ["PNode", "PPathNode", "PProvisionalNode", "PTask", "PTaskWithPath"] |
| 19 | +__all__ = [ |
| 20 | + "NodeTree", |
| 21 | + "PNode", |
| 22 | + "PPathNode", |
| 23 | + "PProvisionalNode", |
| 24 | + "PTask", |
| 25 | + "PTaskWithPath", |
| 26 | + "TaskIO", |
| 27 | + "TaskNode", |
| 28 | +] |
18 | 29 |
|
19 | 30 |
|
20 | 31 | @runtime_checkable |
@@ -64,45 +75,6 @@ class PPathNode(PNode, Protocol): |
64 | 75 | path: NodePath |
65 | 76 |
|
66 | 77 |
|
67 | | -@runtime_checkable |
68 | | -class PTask(Protocol): |
69 | | - """Protocol for nodes.""" |
70 | | - |
71 | | - name: str |
72 | | - depends_on: dict[str, PyTree[PNode | PProvisionalNode]] |
73 | | - produces: dict[str, PyTree[PNode | PProvisionalNode]] |
74 | | - function: Callable[..., Any] |
75 | | - markers: list[Mark] |
76 | | - report_sections: list[tuple[str, str, str]] |
77 | | - attributes: dict[Any, Any] |
78 | | - |
79 | | - @property |
80 | | - def signature(self) -> str: |
81 | | - """Return the signature of the node.""" |
82 | | - |
83 | | - def state(self) -> str | None: |
84 | | - """Return the state of the node. |
85 | | -
|
86 | | - The state can be something like a hash or a last modified timestamp. If the node |
87 | | - does not exist, you can also return ``None``. |
88 | | -
|
89 | | - """ |
90 | | - |
91 | | - def execute(self, **kwargs: Any) -> Any: |
92 | | - """Return the value of the node that will be injected into the task.""" |
93 | | - |
94 | | - |
95 | | -@runtime_checkable |
96 | | -class PTaskWithPath(PTask, Protocol): |
97 | | - """Tasks with paths. |
98 | | -
|
99 | | - Tasks with paths receive special handling when it comes to printing their names. |
100 | | -
|
101 | | - """ |
102 | | - |
103 | | - path: Path |
104 | | - |
105 | | - |
106 | 78 | @runtime_checkable |
107 | 79 | class PProvisionalNode(Protocol): |
108 | 80 | """A protocol for provisional nodes. |
@@ -141,3 +113,52 @@ def load(self, is_product: bool = False) -> Any: # pragma: no cover |
141 | 113 |
|
142 | 114 | def collect(self) -> list[Any]: |
143 | 115 | """Collect the objects that are defined by the provisional nodes.""" |
| 116 | + |
| 117 | + |
| 118 | +TaskNode: TypeAlias = PNode | PProvisionalNode |
| 119 | +"""A concrete or provisional pytask node.""" |
| 120 | + |
| 121 | +NodeTree: TypeAlias = PyTree[TaskNode] |
| 122 | +"""A pytask tree whose leaves are concrete or provisional nodes.""" |
| 123 | + |
| 124 | +TaskIO: TypeAlias = dict[str, NodeTree] |
| 125 | +"""The top-level task argument mapping for dependencies and products.""" |
| 126 | + |
| 127 | + |
| 128 | +@runtime_checkable |
| 129 | +class PTask(Protocol): |
| 130 | + """Protocol for nodes.""" |
| 131 | + |
| 132 | + name: str |
| 133 | + depends_on: TaskIO |
| 134 | + produces: TaskIO |
| 135 | + function: Callable[..., Any] |
| 136 | + markers: list[Mark] |
| 137 | + report_sections: list[tuple[str, str, str]] |
| 138 | + attributes: dict[Any, Any] |
| 139 | + |
| 140 | + @property |
| 141 | + def signature(self) -> str: |
| 142 | + """Return the signature of the node.""" |
| 143 | + |
| 144 | + def state(self) -> str | None: |
| 145 | + """Return the state of the node. |
| 146 | +
|
| 147 | + The state can be something like a hash or a last modified timestamp. If the node |
| 148 | + does not exist, you can also return ``None``. |
| 149 | +
|
| 150 | + """ |
| 151 | + |
| 152 | + def execute(self, **kwargs: Any) -> Any: |
| 153 | + """Return the value of the node that will be injected into the task.""" |
| 154 | + |
| 155 | + |
| 156 | +@runtime_checkable |
| 157 | +class PTaskWithPath(PTask, Protocol): |
| 158 | + """Tasks with paths. |
| 159 | +
|
| 160 | + Tasks with paths receive special handling when it comes to printing their names. |
| 161 | +
|
| 162 | + """ |
| 163 | + |
| 164 | + path: Path |
0 commit comments