-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipeline_test.py
1545 lines (1078 loc) · 39.1 KB
/
pipeline_test.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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
import functools
from dataclasses import dataclass
from typing import Any, Callable, Generic, List, NewType, TypeVar
import numpy as np
import numpy.typing as npt
import pytest
import sciline as sl
def int_to_float(x: int) -> float:
return 0.5 * x
def make_int() -> int:
return 3
def int_float_to_str(x: int, y: float) -> str:
return f"{x};{y}"
def test_pipeline_with_callables_can_compute_single_results() -> None:
pipeline = sl.Pipeline([int_to_float, make_int])
assert pipeline.compute(float) == 1.5
assert pipeline.compute(int) == 3
def test_pipeline_does_not_autobind_types_that_can_be_default_constructed() -> None:
# `int` can be constructed without arguments (and returns 0). Make sure that
# the pipeline does not automatically bind `int` to `0`.
pipeline = sl.Pipeline([int_to_float])
with pytest.raises(sl.UnsatisfiedRequirement):
pipeline.compute(float)
def test_intermediate_used_multiple_times_is_computed_only_once() -> None:
ncall = 0
def provide_int() -> int:
nonlocal ncall
ncall += 1
return 3
pipeline = sl.Pipeline([int_to_float, provide_int, int_float_to_str])
assert pipeline.compute(str) == "3;1.5"
assert ncall == 1
def test_multiple_keys_can_be_computed_without_repeated_calls() -> None:
ncall = 0
def provide_int() -> int:
nonlocal ncall
ncall += 1
return 3
pipeline = sl.Pipeline([int_to_float, provide_int, int_float_to_str])
assert pipeline.compute((float, str)) == {float: 1.5, str: "3;1.5"}
assert ncall == 1
def test_multiple_keys_not_in_same_path_use_same_intermediate() -> None:
ncall = 0
def provide_int() -> int:
nonlocal ncall
ncall += 1
return 3
def func1(x: int) -> float:
return 0.5 * x
def func2(x: int) -> str:
return f"{x}"
pipeline = sl.Pipeline([provide_int, func1, func2])
assert pipeline.compute((float, str)) == {float: 1.5, str: "3"}
assert ncall == 1
def test_Scope_subclass_can_be_set_as_param() -> None:
Param = TypeVar('Param')
class Str(sl.Scope[Param, str], str):
...
pipeline = sl.Pipeline(params={Str[int]: Str[int]('1')})
pipeline[Str[float]] = Str[float]('2.0')
assert pipeline.compute(Str[int]) == Str[int]('1')
assert pipeline.compute(Str[float]) == Str[float]('2.0')
def test_Scope_subclass_can_be_set_as_param_with_unbound_typevar() -> None:
Param = TypeVar('Param')
class Str(sl.Scope[Param, str], str):
...
pipeline = sl.Pipeline()
pipeline[Str[Param]] = Str[Param]('1') # type: ignore[valid-type]
assert pipeline.compute(Str[int]) == Str[int]('1')
assert pipeline.compute(Str[float]) == Str[float]('1')
def test_ScopeTwoParam_subclass_can_be_set_as_param() -> None:
Param1 = TypeVar('Param1')
Param2 = TypeVar('Param2')
class Str(sl.ScopeTwoParams[Param1, Param2, str], str):
...
pipeline = sl.Pipeline(params={Str[int, float]: Str[int, float]('1')})
pipeline[Str[float, int]] = Str[float, int]('2.0')
assert pipeline.compute(Str[int, float]) == Str[int, float]('1')
assert pipeline.compute(Str[float, int]) == Str[float, int]('2.0')
def test_ScopeTwoParam_subclass_can_be_set_as_param_with_unbound_typevar() -> None:
Param1 = TypeVar('Param1')
Param2 = TypeVar('Param2')
class Str(sl.ScopeTwoParams[Param1, Param2, str], str):
...
pipeline = sl.Pipeline()
pipeline[Str[Param1, Param2]] = Str[Param1, Param2]('1') # type: ignore[valid-type]
assert pipeline.compute(Str[int, float]) == Str[int, float]('1')
assert pipeline.compute(Str[float, int]) == Str[float, int]('1')
def test_generic_providers_produce_use_dependencies_based_on_bound_typevar() -> None:
Param = TypeVar('Param')
class Str(sl.Scope[Param, str], str):
...
def parametrized(x: Param) -> Str[Param]:
return Str(f'{x}')
def make_float() -> float:
return 1.5
def combine(x: Str[int], y: Str[float]) -> str:
return f"{x};{y}"
pipeline = sl.Pipeline([make_int, make_float, combine, parametrized])
assert pipeline.compute(Str[int]) == Str[int]('3')
assert pipeline.compute(Str[float]) == Str[float]('1.5')
assert pipeline.compute(str) == '3;1.5'
def test_can_compute_result_depending_on_two_instances_of_generic_provider() -> None:
ncall = 0
def provide_int() -> int:
nonlocal ncall
ncall += 1
return 3
Param = TypeVar('Param')
class Float(sl.Scope[Param, float], float):
...
class Str(sl.Scope[Param, str], str):
...
def int_float_to_str(x: int, y: Float[Param]) -> Str[Param]:
return Str(f"{x};{y}")
Run1 = NewType('Run1', int)
Run2 = NewType('Run2', int)
Result = NewType('Result', str)
def float1() -> Float[Run1]:
return Float[Run1](1.5)
def float2() -> Float[Run2]:
return Float[Run2](2.5)
def use_strings(s1: Str[Run1], s2: Str[Run2]) -> Result:
return Result(f"{s1};{s2}")
pipeline = sl.Pipeline(
[provide_int, float1, float2, use_strings, int_float_to_str],
)
assert pipeline.compute(Result) == "3;1.5;3;2.5"
assert ncall == 1
def test_subclasses_of_generic_provider_defined_with_Scope_work() -> None:
Param = TypeVar('Param')
class StrT(sl.Scope[Param, str], str):
...
class Str1(StrT[Param]):
...
class Str2(StrT[Param]):
...
class Str3(StrT[Param]):
...
class Str4(Str3[Param]):
...
def make_str1() -> Str1[Param]:
return Str1('1')
def make_str2() -> Str2[Param]:
return Str2('2')
# Note that mypy cannot detect if when setting params, the type of the
# parameter does not match the key. Same problem as with NewType.
pipeline = sl.Pipeline(
[make_str1, make_str2],
params={
Str3[int]: Str3[int]('int3'),
Str3[float]: Str3[float]('float3'),
Str4[int]: Str2[int]('int4'),
},
)
assert pipeline.compute(Str1[float]) == Str1[float]('1')
assert pipeline.compute(Str2[float]) == Str2[float]('2')
assert pipeline.compute(Str3[int]) == Str3[int]('int3')
assert pipeline.compute(Str3[float]) == Str3[float]('float3')
assert pipeline.compute(Str4[int]) == Str4[int]('int4')
def test_subclasses_of_generic_array_provider_defined_with_Scope_work() -> None:
Param = TypeVar('Param')
class ArrayT(sl.Scope[Param, npt.NDArray[np.int64]], npt.NDArray[np.int64]):
...
class Array1(ArrayT[Param]):
...
class Array2(ArrayT[Param]):
...
def make_array1() -> Array1[Param]:
return Array1(np.array([1, 2, 3]))
def make_array2() -> Array2[Param]:
return Array2(np.array([4, 5, 6]))
pipeline = sl.Pipeline([make_array1, make_array2])
# Note that the param is not the dtype
assert np.all(pipeline.compute(Array1[str]) == np.array([1, 2, 3]))
assert np.all(pipeline.compute(Array2[str]) == np.array([4, 5, 6]))
def test_inserting_provider_returning_None_raises() -> None:
def provide_none() -> None:
return None
with pytest.raises(ValueError):
sl.Pipeline([provide_none])
pipeline = sl.Pipeline([])
with pytest.raises(ValueError):
pipeline.insert(provide_none)
def test_inserting_provider_with_no_return_type_raises() -> None:
def provide_none(): # type: ignore[no-untyped-def]
return None
with pytest.raises(ValueError):
sl.Pipeline([provide_none])
pipeline = sl.Pipeline([])
with pytest.raises(ValueError):
pipeline.insert(provide_none)
def test_TypeVar_requirement_of_provider_can_be_bound() -> None:
T = TypeVar('T')
def provider_int() -> int:
return 3
def provider(x: T) -> List[T]:
return [x, x]
pipeline = sl.Pipeline([provider_int, provider])
assert pipeline.compute(List[int]) == [3, 3]
def test_TypeVar_that_cannot_be_bound_raises_UnboundTypeVar() -> None:
T = TypeVar('T')
def provider(_: T) -> int:
return 1
pipeline = sl.Pipeline([provider])
with pytest.raises(sl.UnboundTypeVar):
pipeline.compute(int)
def test_unsatisfiable_TypeVar_requirement_of_provider_raises() -> None:
T = TypeVar('T')
def provider_int() -> int:
return 3
def provider(x: T) -> List[T]:
return [x, x]
pipeline = sl.Pipeline([provider_int, provider])
with pytest.raises(sl.UnsatisfiedRequirement):
pipeline.compute(List[float])
def test_TypeVar_params_are_not_associated_unless_they_match() -> None:
T1 = TypeVar('T1')
T2 = TypeVar('T2')
class A(Generic[T1]):
...
class B(Generic[T2]):
...
def source() -> A[int]:
return A[int]()
def not_matching(x: A[T1]) -> B[T2]:
return B[T2]()
def matching(x: A[T1]) -> B[T1]:
return B[T1]()
pipeline = sl.Pipeline([source, not_matching])
with pytest.raises(sl.UnboundTypeVar):
pipeline.compute(B[int])
pipeline = sl.Pipeline([source, matching])
pipeline.compute(B[int])
def test_multi_Generic_with_fully_bound_arguments() -> None:
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class A(Generic[T1, T2]):
first: T1
second: T2
def source() -> A[int, float]:
return A[int, float](1, 2.0)
pipeline = sl.Pipeline([source])
assert pipeline.compute(A[int, float]) == A[int, float](1, 2.0)
def test_multi_Generic_with_partially_bound_arguments() -> None:
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class A(Generic[T1, T2]):
first: T1
second: T2
def source() -> float:
return 2.0
def partially_bound(x: T1) -> A[int, T1]:
return A[int, T1](1, x)
pipeline = sl.Pipeline([source, partially_bound])
assert pipeline.compute(A[int, float]) == A[int, float](1, 2.0)
def test_multi_Generic_with_multiple_unbound() -> None:
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class A(Generic[T1, T2]):
first: T1
second: T2
def int_source() -> int:
return 1
def float_source() -> float:
return 2.0
def unbound(x: T1, y: T2) -> A[T1, T2]:
return A[T1, T2](x, y)
pipeline = sl.Pipeline([int_source, float_source, unbound])
assert pipeline.compute(A[int, float]) == A[int, float](1, 2.0)
assert pipeline.compute(A[float, int]) == A[float, int](2.0, 1)
def test_distinct_fully_bound_instances_yield_distinct_results() -> None:
T1 = TypeVar('T1')
@dataclass
class A(Generic[T1]):
value: T1
def int_source() -> A[int]:
return A[int](1)
def float_source() -> A[float]:
return A[float](2.0)
pipeline = sl.Pipeline([int_source, float_source])
assert pipeline.compute(A[int]) == A[int](1)
assert pipeline.compute(A[float]) == A[float](2.0)
def test_distinct_partially_bound_instances_yield_distinct_results() -> None:
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class A(Generic[T1, T2]):
first: T1
second: T2
def str_source() -> str:
return 'a'
def int_source(x: T1) -> A[int, T1]:
return A[int, T1](1, x)
def float_source(x: T1) -> A[float, T1]:
return A[float, T1](2.0, x)
pipeline = sl.Pipeline([str_source, int_source, float_source])
assert pipeline.compute(A[int, str]) == A[int, str](1, 'a')
assert pipeline.compute(A[float, str]) == A[float, str](2.0, 'a')
def test_multiple_matching_partial_providers_raises() -> None:
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class A(Generic[T1, T2]):
first: T1
second: T2
def int_source() -> int:
return 1
def float_source() -> float:
return 2.0
def provider1(x: T1) -> A[int, T1]:
return A[int, T1](1, x)
def provider2(x: T2) -> A[T2, float]:
return A[T2, float](x, 2.0)
pipeline = sl.Pipeline([int_source, float_source, provider1, provider2])
assert pipeline.compute(A[int, int]) == A[int, int](1, 1)
assert pipeline.compute(A[float, float]) == A[float, float](2.0, 2.0)
with pytest.raises(sl.AmbiguousProvider):
pipeline.compute(A[int, float])
def test_TypeVar_params_track_to_multiple_sources() -> None:
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class A(Generic[T1]):
value: T1
@dataclass
class B(Generic[T1]):
value: T1
@dataclass
class C(Generic[T1, T2]):
first: T1
second: T2
def provide_int() -> int:
return 1
def provide_float() -> float:
return 2.0
def provide_A(x: T1) -> A[T1]:
return A[T1](x)
# Note that it currently does not matter which TypeVar instance we use here:
# Container tracks uses of TypeVar within a single provider, but does not carry
# the information beyond the scope of a single call.
def provide_B(x: T1) -> B[T1]:
return B[T1](x)
def provide_C(x: A[T1], y: B[T2]) -> C[T1, T2]:
return C[T1, T2](x.value, y.value)
pipeline = sl.Pipeline(
[provide_int, provide_float, provide_A, provide_B, provide_C]
)
assert pipeline.compute(C[int, float]) == C[int, float](1, 2.0)
def test_instance_provider() -> None:
Result = NewType('Result', float)
def f(x: int, y: float) -> Result:
return Result(x / y)
pl = sl.Pipeline([f])
pl[int] = 3
pl[float] = 2.0
assert pl.compute(int) == 3
assert pl.compute(float) == 2.0
assert pl.compute(Result) == 1.5
def test_provider_NewType_instance() -> None:
A = NewType('A', int)
pl = sl.Pipeline([])
pl[A] = A(3)
assert pl.compute(A) == 3
def test_setitem_generic_sets_up_working_subproviders() -> None:
T = TypeVar('T')
@dataclass
class A(Generic[T]):
value: T
pl = sl.Pipeline()
pl[A[int]] = A[int](3)
pl[A[float]] = A[float](2.0)
assert pl.compute(A[int]) == A[int](3)
assert pl.compute(A[float]) == A[float](2.0)
with pytest.raises(sl.UnsatisfiedRequirement):
pl.compute(A[str])
def test_setitem_generic_works_without_params() -> None:
T = TypeVar('T')
@dataclass
class A(Generic[T]):
value: T
pl = sl.Pipeline()
pl[A] = A(3)
assert pl.compute(A) == A(3)
def test_setitem_raises_TypeError_if_instance_does_not_match_key() -> None:
A = NewType('A', int)
T = TypeVar('T')
@dataclass
class B(Generic[T]):
value: T
pl = sl.Pipeline()
with pytest.raises(TypeError):
pl[int] = 1.0
with pytest.raises(TypeError):
pl[A] = 1.0
with pytest.raises(TypeError):
pl[B[int]] = 1.0
def test_setitem_can_replace_param_with_param() -> None:
pl = sl.Pipeline()
pl[int] = 1
pl[int] = 2
assert pl.compute(int) == 2
def test_insert_can_replace_param_with_provider() -> None:
def func() -> int:
return 2
pl = sl.Pipeline()
pl[int] = 1
pl.insert(func)
assert pl.compute(int) == 2
def test_setitem_can_replace_provider_with_param() -> None:
def func() -> int:
return 2
pl = sl.Pipeline()
pl.insert(func)
pl[int] = 1
assert pl.compute(int) == 1
def test_insert_can_replace_provider_with_provider() -> None:
def func1() -> int:
return 1
def func2() -> int:
return 2
pl = sl.Pipeline()
pl.insert(func1)
pl.insert(func2)
assert pl.compute(int) == 2
def test_insert_can_replace_generic_provider_with_generic_provider() -> None:
T = TypeVar('T', int, float)
@dataclass
class A(Generic[T]):
value: T
def func1(x: T) -> A[T]:
return A[T](x)
def func2(x: T) -> A[T]:
return A[T](x + x)
pl = sl.Pipeline()
pl[int] = 1
pl.insert(func1)
pl.insert(func2)
assert pl.compute(A[int]) == A[int](2)
def test_insert_can_replace_generic_param_with_generic_provider() -> None:
T = TypeVar('T', int, float)
@dataclass
class A(Generic[T]):
value: T
def func(x: T) -> A[T]:
return A[T](x + x)
pl = sl.Pipeline()
pl[int] = 1
pl[A[T]] = A[T](1) # type: ignore[valid-type]
assert pl.compute(A[int]) == A[int](1)
pl.insert(func)
assert pl.compute(A[int]) == A[int](2)
def test_setitem_can_replace_generic_provider_with_generic_param() -> None:
T = TypeVar('T', int, float)
@dataclass
class A(Generic[T]):
value: T
def func(x: T) -> A[T]:
return A[T](x + x)
pl = sl.Pipeline()
pl[int] = 1
pl.insert(func)
assert pl.compute(A[int]) == A[int](2)
pl[A[T]] = A[T](1) # type: ignore[valid-type]
assert pl.compute(A[int]) == A[int](1)
def test_setitem_can_replace_generic_param_with_generic_param() -> None:
T = TypeVar('T')
@dataclass
class A(Generic[T]):
value: T
pl = sl.Pipeline()
pl[A[T]] = A[T](1) # type: ignore[valid-type]
assert pl.compute(A[int]) == A[int](1)
pl[A[T]] = A[T](2) # type: ignore[valid-type]
assert pl.compute(A[int]) == A[int](2)
def test_init_with_params() -> None:
pl = sl.Pipeline(params={int: 1, float: 2.0})
assert pl.compute(int) == 1
assert pl.compute(float) == 2.0
def test_init_with_providers_and_params() -> None:
def func(x: int, y: float) -> str:
return f'{x} {y}'
pl = sl.Pipeline(providers=[func], params={int: 1, float: 2.0})
assert pl.compute(str) == "1 2.0"
def test_init_with_sciline_Scope_subclass_param_works() -> None:
T = TypeVar('T')
class A(sl.Scope[T, int], int):
...
pl = sl.Pipeline(params={A[float]: A(1), A[str]: A(2)})
assert pl.compute(A[float]) == A(1)
assert pl.compute(A[str]) == A(2)
def test_building_graph_with_cycle_succeeds() -> None:
def f(x: int) -> float:
return float(x)
def g(x: float) -> int:
return int(x)
pipeline = sl.Pipeline([f, g])
_ = pipeline.get(int)
def test_computing_graph_with_cycle_raises_CycleError() -> None:
def f(x: int) -> float:
return float(x)
def g(x: float) -> int:
return int(x)
pipeline = sl.Pipeline([f, g])
with pytest.raises(sl.scheduler.CycleError):
pipeline.compute(int)
def test_get_with_single_key_return_task_graph_that_computes_value() -> None:
pipeline = sl.Pipeline([int_to_float, make_int, int_float_to_str])
task = pipeline.get(str)
assert task.compute() == '3;1.5'
@pytest.mark.parametrize('key_type', [tuple, list, iter])
def test_get_with_key_iterable_return_task_graph_that_computes_dict_of_values(
key_type: Callable[[Any], Any],
) -> None:
pipeline = sl.Pipeline([int_to_float, make_int])
task = pipeline.get(key_type((float, int)))
assert task.compute() == {float: 1.5, int: 3}
def test_task_graph_compute_can_override_single_key() -> None:
pipeline = sl.Pipeline([int_to_float, make_int])
task = pipeline.get(float)
assert task.compute(int) == 3
def test_task_graph_compute_can_override_key_tuple() -> None:
pipeline = sl.Pipeline([int_to_float, make_int])
task = pipeline.get(float)
assert task.compute((int, float)) == {int: 3, float: 1.5}
def test_task_graph_compute_raises_if_override_keys_outside_graph() -> None:
pipeline = sl.Pipeline([int_to_float, make_int])
task = pipeline.get(int)
# The pipeline knows how to compute int, but the task graph does not
# as the task graph is fixed at this point.
with pytest.raises(KeyError):
task.compute(float)
def test_get_with_NaiveScheduler() -> None:
pipeline = sl.Pipeline([int_to_float, make_int])
task = pipeline.get(float, scheduler=sl.scheduler.NaiveScheduler())
assert task.compute() == 1.5
def test_compute_with_NaiveScheduler() -> None:
pipeline = sl.Pipeline([int_to_float, make_int])
res = pipeline.compute(float, scheduler=sl.scheduler.NaiveScheduler())
assert res == 1.5
def test_bind_and_call_no_function() -> None:
pipeline = sl.Pipeline([make_int])
assert pipeline.bind_and_call(()) == ()
def test_bind_and_call_function_without_args() -> None:
def func() -> str:
return "func"
pipeline = sl.Pipeline([make_int])
assert pipeline.bind_and_call(func) == "func"
def test_bind_and_call_function_with_1_arg() -> None:
def func(i: int) -> int:
return i * 2
pipeline = sl.Pipeline([make_int])
assert pipeline.bind_and_call(func) == 6
def test_bind_and_call_function_with_2_arg2() -> None:
def func(i: int, f: float) -> float:
return i + f
pipeline = sl.Pipeline([make_int, int_to_float])
assert pipeline.bind_and_call(func) == 4.5
def test_bind_and_call_overrides_default_args() -> None:
def func(i: int, f: float = -0.5) -> float:
return i + f
pipeline = sl.Pipeline([make_int, int_to_float])
assert pipeline.bind_and_call(func) == 4.5
def test_bind_and_call_function_in_iterator() -> None:
def func(i: int) -> int:
return i * 2
pipeline = sl.Pipeline([make_int])
assert pipeline.bind_and_call(iter((func,))) == (6,)
def test_bind_and_call_dataclass_without_args() -> None:
@dataclass
class C:
...
pipeline = sl.Pipeline([make_int])
assert pipeline.bind_and_call(C) == C()
def test_bind_and_call_dataclass_with_1_arg() -> None:
@dataclass
class C:
i: int
pipeline = sl.Pipeline([make_int])
assert pipeline.bind_and_call(C) == C(i=3)
def test_bind_and_call_dataclass_with_2_arg2() -> None:
@dataclass
class C:
i: int
f: float
pipeline = sl.Pipeline([make_int, int_to_float])
assert pipeline.bind_and_call(C) == C(i=3, f=1.5)
def test_bind_and_call_two_functions() -> None:
def func1(i: int) -> int:
return 2 * i
def func2(f: float) -> float:
return f + 1
pipeline = sl.Pipeline([make_int, int_to_float])
assert pipeline.bind_and_call((func1, func2)) == (6, 2.5)
def test_bind_and_call_two_functions_in_iterator() -> None:
def func1(i: int) -> int:
return 2 * i
def func2(f: float) -> float:
return f + 1
pipeline = sl.Pipeline([make_int, int_to_float])
assert pipeline.bind_and_call(iter((func1, func2))) == (6, 2.5)
def test_bind_and_call_function_and_dataclass() -> None:
def func(i: int) -> int:
return 2 * i
@dataclass
class C:
i: int
f: float
pipeline = sl.Pipeline([make_int, int_to_float])
assert pipeline.bind_and_call((func, C)) == (6, C(i=3, f=1.5))
def test_bind_and_call_function_without_return_annotation() -> None:
def func(i: int): # type: ignore[no-untyped-def]
return 2 * i
pipeline = sl.Pipeline([make_int])
assert pipeline.bind_and_call(func) == 6
def test_bind_and_call_generic_function() -> None:
T = TypeVar('T')
A = NewType('A', int)
B = NewType('B', int)
class G(sl.Scope[T, int], int):
...
def func(a: G[A]) -> int:
return -4 * a
pipeline = sl.Pipeline([], params={G[A]: 3, G[B]: 4})
assert pipeline.bind_and_call(func) == -12
def test_bind_and_call_function_runs_at_end() -> None:
calls = []
def a() -> int:
calls.append('a')
return 2
def b() -> float:
calls.append('b')
return 3.1
def c(_i: int) -> None:
calls.append('c')
def d(_f: float) -> None:
calls.append('d')
pipeline = sl.Pipeline([a, b])
pipeline.bind_and_call([c, d])
assert calls.index('a') in (0, 1)
assert calls.index('b') in (0, 1)
assert calls.index('c') in (2, 3)
assert calls.index('d') in (2, 3)
def test_prioritizes_specialized_provider_over_generic() -> None:
A = NewType('A', str)
B = NewType('B', str)
V = TypeVar('V', A, B)
class H(sl.Scope[V, str], str):
pass
def p1(x: V) -> H[V]:
return H[V]("Generic")
def p2(x: B) -> H[B]:
return H[B]("Special")
pl = sl.Pipeline([p1, p2], params={A: 'A', B: 'B'})
assert str(pl.compute(H[A])) == "Generic"
assert str(pl.compute(H[B])) == "Special"
def test_prioritizes_specialized_provider_over_generic_several_typevars() -> None:
A = NewType('A', str)
B = NewType('B', str)
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class C(Generic[T1, T2]):
first: T1
second: T2
third: str
def p1(x: T1, y: T2) -> C[T1, T2]:
return C(x, y, 'generic')
def p2(x: A, y: T2) -> C[A, T2]:
return C(x, y, 'medium generic')
def p3(x: T2, y: B) -> C[T2, B]:
return C(x, y, 'generic medium')
def p4(x: A, y: B) -> C[A, B]:
return C(x, y, 'special')
pl = sl.Pipeline([p1, p2, p3, p4], params={A: A('A'), B: B('B')})
assert pl.compute(C[B, A]) == C('B', 'A', 'generic')
assert pl.compute(C[A, A]) == C('A', 'A', 'medium generic')
assert pl.compute(C[B, B]) == C('B', 'B', 'generic medium')
assert pl.compute(C[A, B]) == C('A', 'B', 'special')
def test_prioritizes_specialized_provider_raises() -> None:
A = NewType('A', str)
B = NewType('B', str)
T1 = TypeVar('T1')
T2 = TypeVar('T2')