-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcollect_utils.py
692 lines (573 loc) · 22 KB
/
collect_utils.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
"""Contains utility functions for :mod:`_pytask.collect`."""
from __future__ import annotations
import itertools
import uuid
import warnings
from pathlib import Path
from typing import Any
from typing import Callable
from typing import Generator
from typing import Iterable
from typing import TYPE_CHECKING
import attrs
from _pytask._inspect import get_annotations
from _pytask.exceptions import NodeNotCollectedError
from _pytask.mark_utils import has_mark
from _pytask.mark_utils import remove_marks
from _pytask.models import NodeInfo
from _pytask.node_protocols import PNode
from _pytask.nodes import PythonNode
from _pytask.shared import find_duplicates
from _pytask.task_utils import parse_keyword_arguments_from_signature_defaults
from _pytask.tree_util import PyTree
from _pytask.tree_util import tree_leaves
from _pytask.tree_util import tree_map
from _pytask.tree_util import tree_map_with_path
from _pytask.typing import no_default
from _pytask.typing import ProductType
from attrs import define
from attrs import field
from typing_extensions import Annotated
from typing_extensions import get_origin
if TYPE_CHECKING:
from _pytask.session import Session
__all__ = [
"depends_on",
"parse_dependencies_from_task_function",
"parse_products_from_task_function",
"parse_nodes",
"produces",
]
def depends_on(objects: PyTree[Any]) -> PyTree[Any]:
"""Specify dependencies for a task.
Parameters
----------
objects
Can be any valid Python object or an iterable of any Python objects. To be
valid, it must be parsed by some hook implementation for the
:func:`_pytask.hookspecs.pytask_collect_node` entry-point.
"""
return objects
def produces(objects: PyTree[Any]) -> PyTree[Any]:
"""Specify products of a task.
Parameters
----------
objects
Can be any valid Python object or an iterable of any Python objects. To be
valid, it must be parsed by some hook implementation for the
:func:`_pytask.hookspecs.pytask_collect_node` entry-point.
"""
return objects
def parse_nodes( # noqa: PLR0913
session: Session,
task_path: Path | None,
task_name: str,
node_path: Path,
obj: Any,
parser: Callable[..., Any],
) -> Any:
"""Parse nodes from object."""
arg_name = parser.__name__
objects = _extract_nodes_from_function_markers(obj, parser)
nodes = _convert_objects_to_node_dictionary(objects, arg_name)
return tree_map(
lambda x: _collect_decorator_node(
session,
node_path,
task_name,
NodeInfo(
arg_name=arg_name,
path=(),
value=x,
task_path=task_path,
task_name=task_name,
),
),
nodes,
)
def _extract_nodes_from_function_markers(
function: Callable[..., Any], parser: Callable[..., Any]
) -> Generator[Any, None, None]:
"""Extract nodes from a marker.
The parser is a functions which is used to document the marker with the correct
signature. Using the function as a parser for the ``args`` and ``kwargs`` of the
marker provides the expected error message for misspecification.
"""
marker_name = parser.__name__
_, markers = remove_marks(function, marker_name)
for marker in markers:
parsed = parser(*marker.args, **marker.kwargs)
yield parsed
def _convert_objects_to_node_dictionary(objects: Any, when: str) -> dict[Any, Any]:
"""Convert objects to node dictionary."""
list_of_dicts = [_convert_to_dict(x) for x in objects]
_check_that_names_are_not_used_multiple_times(list_of_dicts, when)
return _merge_dictionaries(list_of_dicts)
@define(frozen=True)
class _Placeholder:
"""A placeholder to mark unspecified keys in dictionaries."""
scalar: bool = field(default=False)
id_: uuid.UUID = field(factory=uuid.uuid4)
def _convert_to_dict(x: Any, first_level: bool = True) -> Any | dict[Any, Any]:
"""Convert any object to a dictionary."""
if isinstance(x, dict):
return {k: _convert_to_dict(v, False) for k, v in x.items()}
if isinstance(x, Iterable) and not isinstance(x, str):
if first_level:
return {
_Placeholder(): _convert_to_dict(element, False)
for i, element in enumerate(x)
}
return {i: _convert_to_dict(element, False) for i, element in enumerate(x)}
if first_level:
return {_Placeholder(scalar=True): x}
return x
def _check_that_names_are_not_used_multiple_times(
list_of_dicts: list[dict[Any, Any]], when: str
) -> None:
"""Check that names of nodes are not assigned multiple times.
Tuples in the list have either one or two elements. The first element in the two
element tuples is the name and cannot occur twice.
"""
names_with_provisional_keys = list(
itertools.chain.from_iterable(dict_.keys() for dict_ in list_of_dicts)
)
names = [x for x in names_with_provisional_keys if not isinstance(x, _Placeholder)]
duplicated = find_duplicates(names)
if duplicated:
msg = f"'@pytask.mark.{when}' has nodes with the same name: {duplicated}"
raise ValueError(msg)
def _union_of_dictionaries(dicts: list[dict[Any, Any]]) -> dict[Any, Any]:
"""Merge multiple dictionaries in one.
Examples
--------
>>> a, b = {"a": 0}, {"b": 1}
>>> _union_of_dictionaries([a, b])
{'a': 0, 'b': 1}
>>> a, b = {'a': 0}, {'a': 1}
>>> _union_of_dictionaries([a, b])
{'a': 1}
"""
return dict(itertools.chain.from_iterable(dict_.items() for dict_ in dicts))
def _merge_dictionaries(list_of_dicts: list[dict[Any, Any]]) -> dict[Any, Any]:
"""Merge multiple dictionaries.
The function does not perform a deep merge. It simply merges the dictionary based on
the first level keys which are either unique names or placeholders. During the merge
placeholders will be replaced by an incrementing integer.
Examples
--------
>>> a, b = {"a": 0}, {"b": 1}
>>> _merge_dictionaries([a, b])
{'a': 0, 'b': 1}
>>> a, b = {_Placeholder(): 0}, {_Placeholder(): 1}
>>> _merge_dictionaries([a, b])
{0: 0, 1: 1}
"""
merged_dict = _union_of_dictionaries(list_of_dicts)
if len(merged_dict) == 1 and isinstance(next(iter(merged_dict)), _Placeholder):
placeholder, value = next(iter(merged_dict.items()))
out = value if placeholder.scalar else {0: value}
else:
counter = itertools.count()
out = {}
for k, v in merged_dict.items():
if isinstance(k, _Placeholder):
while True:
possible_key = next(counter)
if possible_key not in merged_dict and possible_key not in out:
out[possible_key] = v
break
else:
out[k] = v
return out
_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": ...})'
- or with the deprecated '@pytask.mark.depends_on' and a 'depends_on' function argument.
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."""
has_depends_on_decorator = False
has_depends_on_argument = False
dependencies = {}
if has_mark(obj, "depends_on"):
has_depends_on_decorator = True
nodes = parse_nodes(session, task_path, task_name, node_path, obj, depends_on)
dependencies["depends_on"] = nodes
task_kwargs = obj.pytask_meta.kwargs if hasattr(obj, "pytask_meta") else {}
signature_defaults = parse_keyword_arguments_from_signature_defaults(obj)
kwargs = {**signature_defaults, **task_kwargs}
kwargs.pop("produces", None)
# Parse products from task decorated with @task and that uses produces.
if "depends_on" in kwargs:
has_depends_on_argument = True
dependencies["depends_on"] = tree_map(
lambda x: _collect_decorator_node(
session,
node_path,
task_name,
NodeInfo(
arg_name="depends_on",
path=(),
value=x,
task_path=task_path,
task_name=task_name,
),
),
kwargs["depends_on"],
)
if has_depends_on_decorator and has_depends_on_argument:
raise NodeNotCollectedError(_ERROR_MULTIPLE_DEPENDENCY_DEFINITIONS)
parameters_with_product_annot = _find_args_with_product_annotation(obj)
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 "
"'@pytask.mark.task(kwargs=...)' and in the type annotation. Choose "
"only one option."
)
raise ValueError(msg)
for parameter_name, value in kwargs.items():
if (
parameter_name in parameters_with_product_annot
or parameter_name == "return"
):
continue
if parameter_name == "depends_on":
continue
nodes = tree_map_with_path(
lambda p, x: _collect_dependency(
session,
node_path,
task_name,
NodeInfo(
arg_name=parameter_name, # noqa: B023
path=p,
value=x,
task_path=task_path,
task_name=task_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) 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]:
"""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_PRODUCT_DEFINITIONS = """The task uses multiple ways to define \
products. Products should be defined with either
- 'typing.Annotated[Path(...), Product]' (recommended)
- '@pytask.mark.task(kwargs={'produces': Path(...)})'
- as a default argument for 'produces': 'produces = Path(...)'
- '@pytask.mark.produces(Path(...))' (deprecated)
Read more about products in the documentation: https://tinyurl.com/pytask-deps-prods.
"""
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 were used to specify products.
"""
has_produces_decorator = False
has_produces_argument = False
has_annotation = False
has_return = False
has_task_decorator = False
out = {}
# Parse products from decorators.
if has_mark(obj, "produces"):
has_produces_decorator = True
nodes = parse_nodes(session, task_path, task_name, node_path, obj, produces)
out = {"produces": nodes}
task_kwargs = obj.pytask_meta.kwargs if hasattr(obj, "pytask_meta") else {}
signature_defaults = parse_keyword_arguments_from_signature_defaults(obj)
kwargs = {**signature_defaults, **task_kwargs}
parameters_with_product_annot = _find_args_with_product_annotation(obj)
parameters_with_node_annot = _find_args_with_node_annotation(obj)
# Parse products from task decorated with @task and that uses produces.
if "produces" in kwargs:
has_produces_argument = True
collected_products = tree_map_with_path(
lambda p, x: _collect_product(
session,
node_path,
task_name,
NodeInfo(
arg_name="produces",
path=p,
value=x,
task_path=task_path,
task_name=task_name,
),
is_string_allowed=True,
),
kwargs["produces"],
)
out = {"produces": collected_products}
if parameters_with_product_annot:
out = {}
for parameter_name in parameters_with_product_annot:
has_annotation = True
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 '@pytask.mark.task(kwargs=...)' and in the type annotation. "
"Choose only one option."
)
raise ValueError(msg)
value = kwargs.get(parameter_name) or parameters_with_node_annot.get(
parameter_name
)
collected_products = tree_map_with_path(
lambda p, x: _collect_product(
session,
node_path,
task_name,
NodeInfo(
arg_name=parameter_name, # noqa: B023
path=p,
value=x,
task_path=task_path,
task_name=task_name,
),
is_string_allowed=False,
),
value,
)
out[parameter_name] = collected_products
if "return" in parameters_with_node_annot:
has_return = True
collected_products = tree_map_with_path(
lambda p, x: _collect_product(
session,
node_path,
task_name,
NodeInfo(
arg_name="return",
path=p,
value=x,
task_path=task_path,
task_name=task_name,
),
is_string_allowed=False,
),
parameters_with_node_annot["return"],
)
out = {"return": collected_products}
task_produces = obj.pytask_meta.produces if hasattr(obj, "pytask_meta") else None
if task_produces:
has_task_decorator = True
collected_products = tree_map_with_path(
lambda p, x: _collect_product(
session,
node_path,
task_name,
NodeInfo(
arg_name="return",
path=p,
value=x,
task_path=task_path,
task_name=task_name,
),
is_string_allowed=False,
),
task_produces,
)
out = {"return": collected_products}
if (
sum(
(
has_produces_decorator,
has_produces_argument,
has_annotation,
has_return,
has_task_decorator,
)
)
>= 2 # noqa: PLR2004
):
raise NodeNotCollectedError(_ERROR_MULTIPLE_PRODUCT_DEFINITIONS)
return out
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
_ERROR_WRONG_TYPE_DECORATOR = """'@pytask.mark.depends_on', '@pytask.mark.produces', \
and their function arguments can only accept values of type 'str' and 'pathlib.Path' \
or the same values nested in tuples, lists, and dictionaries. Here, {node} has type \
{node_type}.
"""
_WARNING_STRING_DEPRECATED = """Using strings to specify a {kind} is deprecated. Pass \
a 'pathlib.Path' instead with 'Path("{node}")'.
"""
def _collect_decorator_node(
session: Session, path: Path, name: str, node_info: NodeInfo
) -> PNode:
"""Collect nodes for a task.
Raises
------
NodeNotCollectedError
If the node could not collected.
"""
node = node_info.value
kind = {"depends_on": "dependency", "produces": "product"}.get(node_info.arg_name)
if not isinstance(node, (str, Path)):
raise NodeNotCollectedError(
_ERROR_WRONG_TYPE_DECORATOR.format(node=node, node_type=type(node))
)
if isinstance(node, str):
warnings.warn(
_WARNING_STRING_DEPRECATED.format(kind=kind, node=node),
category=FutureWarning,
stacklevel=1,
)
node = Path(node)
node_info = node_info._replace(value=node)
collected_node = session.hook.pytask_collect_node(
session=session, path=path, node_info=node_info
)
if collected_node is None:
msg = f"{node!r} cannot be parsed as a {kind} for task {name!r} in {path!r}."
raise NodeNotCollectedError(msg)
return collected_node
def _collect_dependency(
session: Session, path: Path, name: str, node_info: NodeInfo
) -> PNode:
"""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 = attrs.evolve(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:
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,
is_string_allowed: bool = False,
) -> PNode:
"""Collect products for a task.
Defining products with strings is only allowed when using the decorator. Parameter
defaults can only be :class:`pathlib.Path`s.
Raises
------
NodeNotCollectedError
If the node could not collected.
"""
node = node_info.value
# For historical reasons, task.kwargs is like the deco and supports str and Path.
if not isinstance(node, (str, Path)) and is_string_allowed:
msg = (
f"`@pytask.mark.task(kwargs={{'produces': ...}}` can only accept values of "
"type 'str' and 'pathlib.Path' or the same values nested in tuples, lists, "
f"and dictionaries. Here, {node} has type {type(node)}."
)
raise ValueError(msg)
# If we encounter a string and it is allowed, convert it to a path.
if isinstance(node, str) and is_string_allowed:
node = Path(node)
node_info = node_info._replace(value=node)
collected_node = session.hook.pytask_collect_node(
session=session, path=path, node_info=node_info
)
if collected_node is None:
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."""
prefix = (
node_info.task_path.as_posix() + "::" + node_info.task_name
if node_info.task_path
else node_info.task_name
)
node_name = prefix + "::" + node_info.arg_name
if node_info.path:
suffix = "-".join(map(str, node_info.path))
node_name += "::" + suffix
return node_name