-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcollect_utils.py
More file actions
362 lines (295 loc) · 11.4 KB
/
Copy pathcollect_utils.py
File metadata and controls
362 lines (295 loc) · 11.4 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""Contains utility functions for `_pytask.collect`."""
from __future__ import annotations
import inspect
from dataclasses import replace
from typing import TYPE_CHECKING
from typing import Annotated
from typing import Any
from typing import get_origin
from _pytask._inspect import get_annotations
from _pytask.exceptions import NodeNotCollectedError
from _pytask.models import NodeInfo
from _pytask.node_protocols import NodeTree
from _pytask.node_protocols import PNode
from _pytask.node_protocols import PProvisionalNode
from _pytask.nodes import PythonNode
from _pytask.task_utils import parse_keyword_arguments_from_signature_defaults
from _pytask.tree_util import tree_leaves
from _pytask.tree_util import tree_map_with_path
from _pytask.typing import ProductType
from _pytask.typing import TaskFunction
from _pytask.typing import no_default
if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path
from _pytask.session import Session
__all__ = [
"collect_dependency",
"parse_dependencies_from_task_function",
"parse_products_from_task_function",
]
_ERROR_MULTIPLE_DEPENDENCY_DEFINITIONS = """The task uses multiple ways to define \
dependencies. Dependencies should be defined with either
- as default value for the function argument 'depends_on'.
- as '@pytask.task(kwargs={"depends_on": ...})'
Use only one of the two ways!
Hint: You do not need to use 'depends_on' as the argument name since pytask v0.4. \
Every function argument that is not a product is treated as a dependency. Read more \
about dependencies in the documentation: https://tinyurl.com/pytask-deps-prods.
"""
def parse_dependencies_from_task_function(
session: Session, task_path: Path | None, task_name: str, node_path: Path, obj: Any
) -> dict[str, Any]:
"""Parse dependencies from task function."""
dependencies = {}
task_kwargs = obj.pytask_meta.kwargs if isinstance(obj, TaskFunction) else {}
signature_defaults = parse_keyword_arguments_from_signature_defaults(obj)
kwargs = {**signature_defaults, **task_kwargs}
kwargs.pop("produces", None)
parameters_with_product_annot = _find_args_with_product_annotation(obj)
parameters_with_product_annot.append("return")
parameters_with_node_annot = _find_args_with_node_annotation(obj)
# Complete kwargs with node annotations, when no value is given by kwargs.
for name in list(parameters_with_node_annot):
if name not in kwargs:
kwargs[name] = parameters_with_node_annot.pop(name)
else:
msg = (
f"The value for the parameter {name!r} is defined twice, in "
"'@task(kwargs=...)' and in the type annotation. Choose only one way."
)
raise ValueError(msg)
for parameter_name, value in kwargs.items():
if parameter_name in parameters_with_product_annot:
continue
nodes = _collect_nodes_and_provisional_nodes(
collect_dependency,
session,
node_path,
task_name,
task_path,
parameter_name,
value,
)
# If all nodes are python nodes, we simplify the parameter value and store it in
# one node. If it is a node, we keep it.
are_all_nodes_python_nodes_without_hash = all(
isinstance(x, PythonNode) and not x.hash for x in tree_leaves(nodes)
)
if (
not isinstance(nodes, (PNode, PProvisionalNode))
and are_all_nodes_python_nodes_without_hash
):
node_name = create_name_of_python_node(
NodeInfo(
arg_name=parameter_name,
path=(),
value=value,
task_path=task_path,
task_name=task_name,
)
)
dependencies[parameter_name] = PythonNode(value=value, name=node_name)
else:
dependencies[parameter_name] = nodes
return dependencies
def _find_args_with_node_annotation(
func: Callable[..., Any],
) -> dict[str, PNode | PProvisionalNode]:
"""Find args with node annotations."""
annotations = get_annotations(func, eval_str=True)
metas = {
name: annotation.__metadata__
for name, annotation in annotations.items()
if get_origin(annotation) is Annotated
}
args_with_node_annotation = {}
for name, meta in metas.items():
annot = [i for i in meta if not isinstance(i, ProductType)]
if len(annot) >= 2: # noqa: PLR2004
msg = (
f"Parameter {name!r} has multiple node annotations although only one "
f"is allowed. Annotations: {annot}"
)
raise ValueError(msg)
if annot:
args_with_node_annotation[name] = annot[0]
return args_with_node_annotation
_ERROR_MULTIPLE_TASK_RETURN_DEFINITIONS = """The task uses multiple ways to parse \
products from the return of the task function. Use either
def task_example() -> Annotated[str, Path("file.txt")]:
...
or
@task(produces=Path("file.txt"))
def task_example() -> str:
...
Read more about products in the documentation: http://tinyurl.com/pytask-return.
"""
def parse_products_from_task_function(
session: Session, task_path: Path | None, task_name: str, node_path: Path, obj: Any
) -> dict[str, Any]:
"""Parse products from task function.
Raises
------
NodeNotCollectedError
If multiple ways to parse products from the return of the task function are
used.
"""
has_return = False
has_task_decorator = False
out: dict[str, Any] = {}
task_kwargs = obj.pytask_meta.kwargs if isinstance(obj, TaskFunction) else {}
signature_defaults = parse_keyword_arguments_from_signature_defaults(obj)
kwargs = {**signature_defaults, **task_kwargs}
parameters = list(inspect.signature(obj).parameters)
parameters_with_product_annot = _find_args_with_product_annotation(obj)
parameters_with_node_annot = _find_args_with_node_annotation(obj)
# Allow to collect products from 'produces'.
if "produces" in parameters and "produces" not in parameters_with_product_annot:
parameters_with_product_annot.append("produces")
if "return" in parameters_with_node_annot:
parameters_with_product_annot.append("return")
has_return = True
if parameters_with_product_annot:
out = {}
for parameter_name in parameters_with_product_annot:
# Makes sure that missing products will show up as missing inputs during the
# execution.
if (
parameter_name not in kwargs
and parameter_name not in parameters_with_node_annot
):
continue
if (
parameter_name in kwargs
and parameter_name in parameters_with_node_annot
):
msg = (
f"The value for the parameter {parameter_name!r} is defined twice, "
"in '@task(kwargs=...)' and in the type annotation. Choose only "
"one way."
)
raise ValueError(msg)
value = kwargs.get(parameter_name) or parameters_with_node_annot.get(
parameter_name
)
collected_products = _collect_nodes_and_provisional_nodes(
_collect_product,
session,
node_path,
task_name,
task_path,
parameter_name,
value,
)
out[parameter_name] = collected_products
task_produces = obj.pytask_meta.produces if isinstance(obj, TaskFunction) else None
if task_produces:
has_task_decorator = True
collected_products = _collect_nodes_and_provisional_nodes(
_collect_product,
session,
node_path,
task_name,
task_path,
"return",
task_produces,
)
out = {"return": collected_products}
if sum((has_return, has_task_decorator)) == 2: # noqa: PLR2004
raise NodeNotCollectedError(_ERROR_MULTIPLE_TASK_RETURN_DEFINITIONS)
return out
def _collect_nodes_and_provisional_nodes( # noqa: PLR0913
collection_func: Callable[..., Any],
session: Session,
node_path: Path,
task_name: str,
task_path: Path | None,
parameter_name: str,
value: Any,
) -> NodeTree:
return tree_map_with_path(
lambda p, x: collection_func(
session,
node_path,
task_name,
NodeInfo(
arg_name=parameter_name,
path=p,
value=x,
task_path=task_path,
task_name=task_name,
),
),
value,
)
def _find_args_with_product_annotation(func: Callable[..., Any]) -> list[str]:
"""Find args with product annotations."""
annotations = get_annotations(func, eval_str=True)
metas = {
name: annotation.__metadata__
for name, annotation in annotations.items()
if get_origin(annotation) is Annotated
}
args_with_product_annot = []
for name, meta in metas.items():
has_product_annot = any(isinstance(i, ProductType) for i in meta)
if has_product_annot:
args_with_product_annot.append(name)
return args_with_product_annot
def collect_dependency(
session: Session, path: Path, name: str, node_info: NodeInfo
) -> PNode | PProvisionalNode:
"""Collect nodes for a task.
Raises
------
NodeNotCollectedError
If the node could not collected.
"""
node = node_info.value
if isinstance(node, PythonNode) and node.value is no_default:
# If a node is a dependency and its value is not set, the node is a product in
# another task and the value will be set there. Thus, we wrap the original node
# in another node to retrieve the value after it is set.
new_node = replace(node, value=node)
node_info = node_info._replace(value=new_node)
collected_node = session.hook.pytask_collect_node(
session=session, path=path, node_info=node_info
)
if collected_node is None: # pragma: no cover
msg = (
f"{node!r} cannot be parsed as a dependency for task {name!r} in {path!r}."
)
raise NodeNotCollectedError(msg)
return collected_node
def _collect_product(
session: Session,
path: Path,
task_name: str,
node_info: NodeInfo,
) -> PNode | PProvisionalNode:
"""Collect products for a task.
Defining products with strings is only allowed when using the decorator. Parameter
defaults can only be `pathlib.Path`s.
Raises
------
NodeNotCollectedError
If the node could not collected.
"""
node = node_info.value
collected_node = session.hook.pytask_collect_node(
session=session, path=path, node_info=node_info
)
if collected_node is None: # pragma: no cover
msg = (
f"{node!r} can't be parsed as a product for task {task_name!r} in {path!r}."
)
raise NodeNotCollectedError(msg)
return collected_node
def create_name_of_python_node(node_info: NodeInfo) -> str:
"""Create name of PythonNode."""
node_name = node_info.task_name + "::" + node_info.arg_name
if node_info.path:
suffix = "-".join(str(p) for p in node_info.path)
node_name += "::" + suffix
return node_name