-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_execute.py
936 lines (716 loc) · 29.8 KB
/
test_execute.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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
from __future__ import annotations
import json
import os
import pickle
import re
import subprocess
import sys
import textwrap
from pathlib import Path
import pytask
import pytest
from pytask import build
from pytask import CaptureMethod
from pytask import cli
from pytask import ExitCode
from pytask import NodeNotFoundError
from pytask import PathNode
from pytask import TaskOutcome
from pytask import TaskWithoutPath
@pytest.mark.xfail(sys.platform == "win32", reason="See #293.")
@pytest.mark.end_to_end()
def test_python_m_pytask(tmp_path):
tmp_path.joinpath("task_module.py").write_text("def task_example(): pass")
subprocess.run(["python", "-m", "pytask", tmp_path.as_posix()], check=False)
@pytest.mark.end_to_end()
def test_execute_w_autocollect(runner, tmp_path):
tmp_path.joinpath("task_module.py").write_text("def task_example(): pass")
cwd = Path.cwd()
os.chdir(tmp_path)
result = runner.invoke(cli)
os.chdir(cwd)
assert result.exit_code == ExitCode.OK
assert "1 Succeeded" in result.output
@pytest.mark.end_to_end()
def test_task_did_not_produce_node(tmp_path):
source = """
from pathlib import Path
def task_example(produces=Path("out.txt")): ...
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
session = build(paths=tmp_path)
assert session.exit_code == ExitCode.FAILED
assert len(session.execution_reports) == 1
assert isinstance(session.execution_reports[0].exc_info[1], NodeNotFoundError)
@pytest.mark.end_to_end()
def test_task_did_not_produce_multiple_nodes_and_all_are_shown(runner, tmp_path):
source = """
from pathlib import Path
def task_example(produces=[Path("1.txt"), Path("2.txt")]): ...
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "NodeNotFoundError" in result.output
assert "1.txt" in result.output
assert "2.txt" in result.output
@pytest.mark.end_to_end()
def test_missing_product(runner, tmp_path):
source = """
from pathlib import Path
from typing_extensions import Annotated
from pytask import Product
def task_with_non_path_dependency(path: Annotated[Path, Product]): ...
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
@pytest.mark.end_to_end()
def test_node_not_found_in_task_setup(tmp_path):
"""Test for :class:`_pytask.exceptions.NodeNotFoundError` in task setup.
Before a task is executed, pytask checks whether all dependencies can be found.
Normally, missing dependencies are caught during resolving dependencies if they are
root nodes or when a task does not produce a node.
To force this error one task accidentally deletes the product of another task.
"""
source = """
import pytask
from typing_extensions import Annotated
from pathlib import Path
def task_1() -> Annotated[None, (Path("out_1.txt"), Path("deleted.txt"))]:
return "", ""
def task_2(path = Path("out_1.txt")) -> Annotated[str, Path("out_2.txt")]:
path.with_name("deleted.txt").unlink()
return ""
def task_3(paths = [Path("deleted.txt"), Path("out_2.txt")]):
...
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
session = build(paths=tmp_path)
assert session.exit_code == ExitCode.FAILED
assert sum(i.outcome == TaskOutcome.SUCCESS for i in session.execution_reports) == 2
report = session.execution_reports[2]
assert report.outcome == TaskOutcome.FAIL
assert isinstance(report.exc_info[1], NodeNotFoundError)
@pytest.mark.end_to_end()
def test_depends_on_and_produces_can_be_used_in_task(tmp_path):
source = """
from pathlib import Path
def task_example(path=Path("in.txt"), produces=Path("out.txt")):
assert isinstance(path, Path) and isinstance(produces, Path)
produces.write_text(path.read_text())
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
tmp_path.joinpath("in.txt").write_text("Here I am. Once again.")
session = build(paths=tmp_path)
assert session.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").read_text() == "Here I am. Once again."
@pytest.mark.end_to_end()
@pytest.mark.parametrize("n_failures", [1, 2, 3])
def test_execution_stops_after_n_failures(tmp_path, n_failures):
source = """
def task_1(): raise Exception
def task_2(): raise Exception
def task_3(): raise Exception
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
session = build(paths=tmp_path, max_failures=n_failures)
assert len(session.tasks) == 3
assert len(session.execution_reports) == n_failures
@pytest.mark.end_to_end()
@pytest.mark.parametrize("stop_after_first_failure", [False, True])
def test_execution_stop_after_first_failure(tmp_path, stop_after_first_failure):
source = """
def task_1(): raise Exception
def task_2(): raise Exception
def task_3(): raise Exception
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
session = build(paths=tmp_path, stop_after_first_failure=stop_after_first_failure)
assert len(session.tasks) == 3
assert len(session.execution_reports) == 1 if stop_after_first_failure else 3
@pytest.mark.end_to_end()
def test_scheduling_w_priorities(tmp_path):
source = """
import pytask
@pytask.mark.try_first
def task_z(): pass
def task_x(): pass
@pytask.mark.try_last
def task_y(): pass
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
session = build(paths=tmp_path)
assert session.exit_code == ExitCode.OK
assert session.execution_reports[0].task.name.endswith("task_z")
assert session.execution_reports[1].task.name.endswith("task_x")
assert session.execution_reports[2].task.name.endswith("task_y")
@pytest.mark.end_to_end()
@pytest.mark.parametrize("show_errors_immediately", [True, False])
def test_show_errors_immediately(runner, tmp_path, show_errors_immediately):
source = """
def task_succeed(): pass
def task_error(): raise ValueError
"""
tmp_path.joinpath("task_error.py").write_text(textwrap.dedent(source))
args = [tmp_path.as_posix()]
if show_errors_immediately:
args.append("--show-errors-immediately")
result = runner.invoke(cli, args)
assert result.exit_code == ExitCode.FAILED
assert "::task_succeed │ ." in result.output
matches_traceback = re.findall("Traceback", result.output)
if show_errors_immediately:
assert len(matches_traceback) == 2
else:
assert len(matches_traceback) == 1
@pytest.mark.end_to_end()
@pytest.mark.parametrize("verbose", [1, 2])
def test_traceback_of_previous_task_failed_is_not_shown(runner, tmp_path, verbose):
source = """
from pathlib import Path
def task_first(produces=Path("in.txt")): raise ValueError
def task_second(path=Path("in.txt")): ...
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix(), "--verbose", str(verbose)])
assert result.exit_code == ExitCode.FAILED
assert ("Task task_example.py::task_second failed" in result.output) is (
verbose == 2
)
@pytest.mark.end_to_end()
def test_that_dynamically_creates_tasks_are_captured(runner, tmp_path):
source = """
_DEFINITION = '''
def task_example():
pass
'''
exec(_DEFINITION)
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "task_example" in result.output
assert "Collected 1 task" in result.output
@pytest.mark.end_to_end()
def test_task_executed_with_force_although_unchanged(tmp_path):
tmp_path.joinpath("task_module.py").write_text("def task_example(): pass")
session = build(paths=tmp_path)
assert session.execution_reports[0].outcome == TaskOutcome.SUCCESS
session = build(paths=tmp_path, force=True)
assert session.execution_reports[0].outcome == TaskOutcome.SUCCESS
@pytest.mark.end_to_end()
def test_task_executed_with_force_although_unchanged_runner(runner, tmp_path):
tmp_path.joinpath("task_module.py").write_text("def task_example(): pass")
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "Collected 1 task" in result.output
assert "1 Succeeded" in result.output
result = runner.invoke(cli, [tmp_path.as_posix(), "--force"])
assert result.exit_code == ExitCode.OK
assert "1 Succeeded" in result.output
@pytest.mark.end_to_end()
def test_task_is_not_reexecuted_when_modification_changed_file_not(runner, tmp_path):
tmp_path.joinpath("task_example.py").write_text("def task_example(): pass")
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "1 Succeeded" in result.output
tmp_path.joinpath("task_example.py").write_text("def task_example(): pass")
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "1 Skipped" in result.output
@pytest.mark.end_to_end()
@pytest.mark.parametrize("arg_name", ["path", "produces"])
def test_task_with_product_annotation(tmp_path, arg_name):
"""Using 'produces' with a product annotation should not cause an error."""
source = f"""
from pathlib import Path
from typing_extensions import Annotated
from pytask import Product
def task_example({arg_name}: Annotated[Path, Product] = Path("out.txt")) -> None:
{arg_name}.touch()
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
session = build(paths=tmp_path, capture=CaptureMethod.NO)
assert session.exit_code == ExitCode.OK
assert len(session.tasks) == 1
task = session.tasks[0]
assert arg_name in task.produces
@pytest.mark.end_to_end()
def test_task_errors_with_nested_product_annotation(tmp_path):
source = """
from pathlib import Path
from typing_extensions import Annotated, Dict
from pytask import Product
def task_example(
paths_to_file: Dict[str, Annotated[Path, Product]] = {"a": Path("out.txt")}
) -> None:
pass
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
session = build(paths=tmp_path, capture=CaptureMethod.NO)
assert session.exit_code == ExitCode.FAILED
assert len(session.tasks) == 1
task = session.tasks[0]
assert "paths_to_file" not in task.produces
@pytest.mark.end_to_end()
@pytest.mark.parametrize(
"definition",
[
" = PythonNode(value=data['dependency'], hash=True)",
": Annotated[Any, PythonNode(value=data['dependency'], hash=True)]",
],
)
def test_task_with_hashed_python_node(runner, tmp_path, definition):
source = f"""
import json
from pathlib import Path
from pytask import Product, PythonNode
from typing_extensions import Annotated, Any
data = json.loads(Path(__file__).parent.joinpath("data.json").read_text())
def task_example(
dependency{definition},
path: Annotated[Path, Product] = Path("out.txt")
) -> None:
path.write_text(dependency)
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
tmp_path.joinpath("data.json").write_text('{"dependency": "hello"}')
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").read_text() == "hello"
tmp_path.joinpath("data.json").write_text('{"dependency": "world"}')
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").read_text() == "world"
@pytest.mark.end_to_end()
def test_return_with_path_annotation_as_return(runner, tmp_path):
source = """
from pathlib import Path
from typing_extensions import Annotated
def task_example() -> Annotated[str, Path("file.txt")]:
return "Hello, World!"
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("file.txt").read_text() == "Hello, World!"
@pytest.mark.end_to_end()
def test_return_with_pathnode_annotation_as_return(runner, tmp_path):
source = """
from pathlib import Path
from typing_extensions import Annotated
from pytask import PathNode
def task_example() -> Annotated[str, PathNode(path=Path("file.txt"))]:
return "Hello, World!"
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("file.txt").read_text() == "Hello, World!"
@pytest.mark.end_to_end()
@pytest.mark.parametrize(
("product_def", "return_def"),
[
("produces=PickleNode(path=Path('data.pkl')))", "produces.save(1)"),
(
"node: Annotated[PickleNode, PickleNode(path=Path('data.pkl')), Product])",
"node.save(1)",
),
(") -> Annotated[int, PickleNode(path=Path('data.pkl'))]", "return 1"),
],
)
def test_custom_node_as_product(runner, tmp_path, product_def, return_def):
source = f"""
from __future__ import annotations
from pathlib import Path
import pickle
from typing import Any
from typing_extensions import Annotated
import attrs
from pytask import Product
@attrs.define
class PickleNode:
path: Path
name: str = ""
signature: str = "id"
def state(self) -> str | None:
if self.path.exists():
return str(self.path.stat().st_mtime)
return None
def load(self, is_product) -> Any:
if is_product:
return self
return pickle.loads(self.path.read_bytes())
def save(self, value: Any) -> None:
self.path.write_bytes(pickle.dumps(value))
def task_example({product_def}:
{return_def}
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
data = pickle.loads(tmp_path.joinpath("data.pkl").read_bytes()) # noqa: S301
assert data == 1
@pytest.mark.end_to_end()
def test_return_with_tuple_pathnode_annotation_as_return(runner, tmp_path):
source = """
from pathlib import Path
from typing_extensions import Annotated
from pytask import PathNode
node1 = PathNode(path=Path("file1.txt"))
node2 = PathNode(path=Path("file2.txt"))
def task_example() -> Annotated[str, (node1, node2)]:
return "Hello,", "World!"
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("file1.txt").read_text() == "Hello,"
assert tmp_path.joinpath("file2.txt").read_text() == "World!"
@pytest.mark.end_to_end()
def test_error_when_return_pytree_mismatch(runner, tmp_path):
source = """
from pathlib import Path
from typing import Any
from typing_extensions import Annotated
from pytask import PathNode
node1 = PathNode(path=Path("file1.txt"))
node2 = PathNode(path=Path("file2.txt"))
def task_example() -> Annotated[str, (node1, node2)]:
return "Hello,"
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "Function return: PyTreeSpec(*, NoneIsLeaf)" in result.output
assert "Return annotation: PyTreeSpec((*, *), NoneIsLeaf)" in result.output
@pytest.mark.end_to_end()
def test_pytree_and_python_node_as_return(runner, tmp_path):
source = """
from pathlib import Path
from typing import Any
from typing_extensions import Annotated
from pytask import PythonNode
from typing import Dict
def task_example() -> Annotated[Dict[str, str], PythonNode(name="result")]:
return {"first": "a", "second": "b"}
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
@pytest.mark.end_to_end()
def test_more_nested_pytree_and_python_node_as_return_with_names(runner, tmp_path):
source = """
from pathlib import Path
from typing import Any
from typing_extensions import Annotated
from pytask import PythonNode
from typing import Dict
nodes = [
PythonNode(name="dict"),
(PythonNode(name="tuple1"), PythonNode(name="tuple2")),
PythonNode(name="int")
]
def task_example() -> Annotated[Dict[str, str], nodes]:
return [{"first": "a", "second": "b"}, (1, 2), 1]
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "1 Succeeded" in result.output
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "1 Skipped" in result.output
@pytest.mark.end_to_end()
def test_more_nested_pytree_and_python_node_as_return(runner, tmp_path):
source = """
from pathlib import Path
from typing import Any
from typing_extensions import Annotated
from pytask import PythonNode
from typing import Dict
nodes = [PythonNode(), (PythonNode(), PythonNode()), PythonNode()]
def task_example() -> Annotated[Dict[str, str], nodes]:
return [{"first": "a", "second": "b"}, (1, 2), 1]
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "1 Succeeded" in result.output
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "1 Skipped" in result.output
@pytest.mark.end_to_end()
def test_execute_tasks_and_pass_values_only_by_python_nodes(runner, tmp_path):
source = """
from pytask import PathNode
from pytask import PythonNode
from typing_extensions import Annotated
from pathlib import Path
node_text = PythonNode(name="text")
def task_create_text() -> Annotated[int, node_text]:
return "This is the text."
node_file = PathNode(path=Path("file.txt"))
def task_create_file(text: Annotated[int, node_text]) -> Annotated[str, node_file]:
return text
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("file.txt").read_text() == "This is the text."
@pytest.mark.end_to_end()
@pytest.mark.xfail(sys.platform == "win32", reason="Decoding issues in Gitlab Actions.")
def test_execute_tasks_via_functional_api(tmp_path):
source = """
import sys
from pathlib import Path
from typing_extensions import Annotated
from pytask import PathNode, PythonNode, build
node_text = PythonNode()
def create_text() -> Annotated[int, node_text]:
return "This is the text."
node_file = PathNode(path=Path("file.txt"))
def create_file(content: Annotated[str, node_text]) -> Annotated[str, node_file]:
return content
if __name__ == "__main__":
session = build(tasks=[create_file, create_text])
assert len(session.tasks) == 2
assert len(session.dag.nodes) == 5
sys.exit(session.exit_code)
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = subprocess.run(
("python", tmp_path.joinpath("task_module.py").as_posix()), check=False
)
assert result.returncode == ExitCode.OK
assert tmp_path.joinpath("file.txt").read_text() == "This is the text."
@pytest.mark.end_to_end()
def test_pass_non_task_to_functional_api_that_are_ignored():
session = pytask.build(tasks=None)
assert len(session.tasks) == 0
@pytest.mark.end_to_end()
def test_multiple_product_annotations(runner, tmp_path):
source = """
from pytask import Product
from typing_extensions import Annotated
from pathlib import Path
def task_first(
first: Annotated[Path, Product] = Path("first.txt"),
second: Annotated[Path, Product] = Path("second.txt")
):
first.write_text("first")
second.write_text("second")
def task_second(
first: Path = Path("first.txt"), second: Path = Path("second.txt")
):
pass
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
@pytest.mark.end_to_end()
def test_errors_during_loading_nodes_have_info(runner, tmp_path):
source = """
from __future__ import annotations
from pathlib import Path
from typing import Any
import attrs
import pickle
@attrs.define
class PickleNode:
name: str
path: Path
signature: str = "id"
def state(self) -> str | None:
if self.path.exists():
return str(self.path.stat().st_mtime)
return None
def load(self) -> Any:
return pickle.loads(self.path.read_bytes())
def save(self, value: Any) -> None:
self.path.write_bytes(pickle.dumps(value))
def task_example(
value=PickleNode(name="node", path=Path(__file__).parent / "file.txt")
): pass
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
tmp_path.joinpath("file.txt").touch()
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "task_example.py::task_example" in result.output
assert "Exception while loading node" in result.output
# Test that traceback is hidden.
assert "_pytask/execute.py" not in result.output
def test_hashing_works(tmp_path):
"""Use subprocess or otherwise the cache is filled from other tests."""
source = """
from pathlib import Path
from typing_extensions import Annotated
def task_example() -> Annotated[str, Path("file.txt")]:
return "Hello, World!"
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = subprocess.run(("pytask"), cwd=tmp_path) # noqa: PLW1510
assert result.returncode == ExitCode.OK
hashes = json.loads(tmp_path.joinpath(".pytask", "file_hashes.json").read_text())
assert len(hashes) == 2
result = subprocess.run(("pytask"), cwd=tmp_path) # noqa: PLW1510
assert result.returncode == ExitCode.OK
hashes_ = json.loads(tmp_path.joinpath(".pytask", "file_hashes.json").read_text())
assert hashes == hashes_
def test_python_node_as_product_with_product_annotation(runner, tmp_path):
source = """
from typing_extensions import Annotated
from pytask import Product, PythonNode
from pathlib import Path
node = PythonNode()
def task_create_string(node: Annotated[PythonNode, node, Product]) -> None:
node.save("Hello, World!")
def task_write_file(text: Annotated[str, node]) -> Annotated[str, Path("file.txt")]:
return text
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("file.txt").read_text() == "Hello, World!"
def test_pickle_node_as_product_with_product_annotation(runner, tmp_path):
source = """
from typing_extensions import Annotated
from pytask import Product, PickleNode
from pathlib import Path
node = PickleNode(name="node", path=Path(__file__).parent / "file.pkl")
def task_create_string(node: Annotated[PickleNode, node, Product]) -> None:
node.save("Hello, World!")
def task_write_file(text: Annotated[str, node]) -> Annotated[str, Path("file.txt")]:
return text
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("file.txt").read_text() == "Hello, World!"
@pytest.mark.end_to_end()
def test_check_if_root_nodes_are_available(tmp_path, runner):
source = """
from pathlib import Path
def task_d(path=Path("in.txt"), produces=Path("out.txt")):
produces.write_text("1")
"""
tmp_path.joinpath("task_d.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "NodeNotFoundError: 'task_d.py::task_d' requires" in result.output
@pytest.mark.end_to_end()
def test_check_if_root_nodes_are_available_w_name(tmp_path, runner):
source = """
from pathlib import Path
from typing_extensions import Annotated, Any
from pytask import PathNode, PythonNode
node1 = PathNode(name="input1", path=Path(__file__).parent / "in.txt")
node2 = PythonNode(name="input2")
def task_e(in1_: Annotated[Path, node1], in2_: Annotated[Any, node2]): ...
"""
tmp_path.joinpath("task_e.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "NodeNotFoundError: 'task_e.py::task_e' requires" in result.output
assert "input1" in result.output
@pytest.mark.end_to_end()
def test_check_if_root_nodes_are_available_with_separate_build_folder(tmp_path, runner):
tmp_path.joinpath("src").mkdir()
tmp_path.joinpath("bld").mkdir()
source = """
from pathlib import Path
def task_d(path=Path("../bld/in.txt"), produces=Path("out.txt")):
produces.write_text("1")
"""
tmp_path.joinpath("src", "task_d.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.joinpath("src").as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "NodeNotFoundError: 'task_d.py::task_d' requires" in result.output
assert "bld/in.txt" in result.output
def test_error_when_node_state_throws_error(runner, tmp_path):
source = """
from pytask import PythonNode
def task_example(a = PythonNode(value={"a": 1}, hash=True)):
pass
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
assert "TypeError: unhashable type: 'dict'" in result.output
def test_task_is_not_reexecuted(runner, tmp_path):
source = """
from typing_extensions import Annotated
from pathlib import Path
def task_first() -> Annotated[str, Path("out.txt")]:
return "Hello, World!"
def task_second(path = Path("out.txt")) -> Annotated[str, Path("copy.txt")]:
return path.read_text()
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "2 Succeeded" in result.output
tmp_path.joinpath("out.txt").write_text("Changed text.")
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert "1 Succeeded" in result.output
assert "1 Skipped because unchanged" in result.output
def test_use_functional_interface_with_task(tmp_path):
def func(path):
path.touch()
task = TaskWithoutPath(
name="task",
function=func,
produces={"path": PathNode(path=tmp_path / "out.txt")},
)
session = build(tasks=[task])
assert session.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").exists()
session = build(tasks=task)
assert session.exit_code == ExitCode.OK
def test_collect_task(runner, tmp_path):
source = """
from pytask import Task, PathNode
from pathlib import Path
def func(path): path.touch()
task_create_file = Task(
base_name="task",
function=func,
path=Path(__file__),
produces={"path": PathNode(path=Path(__file__).parent / "out.txt")},
)
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").exists()
def test_collect_task_without_path(runner, tmp_path):
source = """
from pytask import TaskWithoutPath, PathNode
from pathlib import Path
def func(path): path.touch()
task_create_file = TaskWithoutPath(
name="task",
function=func,
produces={"path": PathNode(path=Path(__file__).parent / "out.txt")},
)
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("out.txt").exists()
def test_with_http_path(runner, tmp_path):
source = """
from upath import UPath
from typing_extensions import Annotated
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
def task_example(path = UPath(url)) -> Annotated[str, UPath("data.txt")]:
return path.read_text()
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))
result = runner.invoke(cli, [tmp_path.as_posix()])
print(result.output) # noqa: T201
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("data.txt").exists()