-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtest_python_fields.py
477 lines (376 loc) · 13.5 KB
/
test_python_fields.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
from operator import attrgetter
import typing as ty
from decimal import Decimal
import attrs
import pytest
from pydra.utils.general import task_fields
from pydra.compose import python
sort_key = attrgetter("name")
def test_interface_wrap_function(tmp_path):
def func(a: int) -> float:
"""Sample function with inputs and outputs"""
return a * 2
SampleDef = python.define(func)
assert issubclass(SampleDef, python.Task)
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int),
python.arg(name="function", type=ty.Callable, hash_eq=True, default=func),
]
assert outputs == [python.out(name="out", type=float)]
task = SampleDef(a=1)
outputs = task(cache_root=tmp_path)
assert outputs.out == 2.0
with pytest.raises(TypeError):
SampleDef(a=1.5)
def test_function_arg_fail():
with pytest.raises(ValueError, match="The argument 'function' is reserved"):
@python.define
def func(function: ty.Callable) -> ty.Callable:
return function
def test_interface_wrap_function_with_default():
def func(a: int, k: float = 2.0) -> float:
"""Sample function with inputs and outputs"""
return a * k
SampleDef = python.define(func)
assert issubclass(SampleDef, python.Task)
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int),
python.arg(name="function", type=ty.Callable, hash_eq=True, default=func),
python.arg(name="k", type=float, default=2.0),
]
assert outputs == [python.out(name="out", type=float)]
assert SampleDef(a=1)().out == 2.0
assert SampleDef(a=10, k=3.0)().out == 30.0
def test_interface_wrap_function_overrides():
def func(a: int) -> float:
"""Sample function with inputs and outputs"""
return a * 2
SampleDef = python.define(
func,
inputs={"a": python.arg(help="The argument to be doubled")},
outputs={"b": python.out(help="the doubled output", type=Decimal)},
)
assert issubclass(SampleDef, python.Task)
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int, help="The argument to be doubled"),
python.arg(name="function", type=ty.Callable, hash_eq=True, default=func),
]
assert outputs == [
python.out(name="b", type=Decimal, help="the doubled output"),
]
outputs = SampleDef.Outputs(b=Decimal(2.0))
assert isinstance(outputs.b, Decimal)
def test_interface_wrap_function_types():
def func(a: int) -> int:
"""Sample function with inputs and outputs"""
return a * 2
SampleDef = python.define(
func,
inputs={"a": float},
outputs={"b": float},
)
assert issubclass(SampleDef, python.Task)
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=float),
python.arg(name="function", type=ty.Callable, hash_eq=True, default=func),
]
assert outputs == [python.out(name="b", type=float)]
intf = SampleDef(a=1)
assert isinstance(intf.a, float)
outputs = SampleDef.Outputs(b=2.0)
assert isinstance(outputs.b, float)
def test_decorated_function_interface():
@python.define(outputs=["c", "d"])
def SampleDef(a: int, b: float) -> tuple[float, float]:
"""Sample function for testing"""
return a + b, a * b
assert issubclass(SampleDef, python.Task)
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int),
python.arg(name="b", type=float),
python.arg(
name="function",
type=ty.Callable,
hash_eq=True,
default=attrs.fields(SampleDef).function.default,
),
]
assert outputs == [
python.out(name="c", type=float),
python.out(name="d", type=float),
]
assert attrs.fields(SampleDef).function.default.__name__ == "SampleDef"
SampleDef.Outputs(c=1.0, d=2.0)
def test_interface_with_function_docstr():
@python.define(outputs=["c", "d"])
def SampleDef(a: int, b: float) -> tuple[float, float]:
"""Sample function for testing
:param a: First input to be inputted
:param b: Second input
:return c: Sum of a and b
:return d: product of a and b
"""
return a + b, a * b
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int, help="First input to be inputted"),
python.arg(name="b", type=float, help="Second input"),
python.arg(
name="function",
type=ty.Callable,
hash_eq=True,
default=attrs.fields(SampleDef).function.default,
),
]
assert outputs == [
python.out(name="c", type=float, help="Sum of a and b"),
python.out(name="d", type=float, help="product of a and b"),
]
assert attrs.fields(SampleDef).function.default.__name__ == "SampleDef"
def test_interface_with_function_google_docstr():
@python.define(outputs=["c", "d"])
def SampleDef(a: int, b: float) -> tuple[float, float]:
"""Sample function for testing
Args:
a: First input
to be inputted
b: Second input
Returns:
c: Sum of a and b
d: Product of a and b
"""
return a + b, a * b
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int, help="First input to be inputted"),
python.arg(name="b", type=float, help="Second input"),
python.arg(
name="function",
type=ty.Callable,
hash_eq=True,
default=attrs.fields(SampleDef).function.default,
),
]
assert outputs == [
python.out(name="c", type=float, help="Sum of a and b"),
python.out(name="d", type=float, help="Product of a and b"),
]
assert attrs.fields(SampleDef).function.default.__name__ == "SampleDef"
def test_interface_with_function_numpy_docstr():
@python.define(
outputs=["c", "d"]
) # Could potentiall read output names from doc-string instead
def SampleDef(a: int, b: float) -> tuple[float, float]:
"""Sample function for testing
Parameters
----------
a: int
First input
to be inputted
b: float
Second input
Returns
-------
c : int
Sum of a and b
d : float
Product of a and b
"""
return a + b, a * b
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int, help="First input to be inputted"),
python.arg(name="b", type=float, help="Second input"),
python.arg(
name="function",
type=ty.Callable,
hash_eq=True,
default=attrs.fields(SampleDef).function.default,
),
]
assert outputs == [
python.out(name="c", type=float, help="Sum of a and b"),
python.out(name="d", type=float, help="Product of a and b"),
]
assert attrs.fields(SampleDef).function.default.__name__ == "SampleDef"
def test_interface_with_class():
@python.define
class SampleDef(python.Task["SampleDef.Outputs"]):
"""Sample class for testing
Args:
a: First input
to be inputted
b: Second input
"""
a: int
b: float = 2.0
class Outputs(python.Outputs):
"""
Args:
c: Sum of a and b
d: Product of a and b
"""
c: float
d: float
@staticmethod
def function(a, b):
return a + b, a * b
assert issubclass(SampleDef, python.Task)
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int, help="First input to be inputted"),
python.arg(name="b", type=float, default=2.0, help="Second input"),
python.arg(
name="function",
type=ty.Callable,
hash_eq=True,
default=attrs.fields(SampleDef).function.default,
),
]
assert outputs == [
python.out(name="c", type=float, help="Sum of a and b"),
python.out(name="d", type=float, help="Product of a and b"),
]
assert SampleDef.function.__name__ == "function"
SampleDef(a=1)
SampleDef(a=1, b=2.0)
SampleDef.Outputs(c=1.0, d=2.0)
def test_interface_with_inheritance():
@python.define
class SampleDef(python.Task["SampleDef.Outputs"]):
"""Sample class for testing
Args:
a: First input
to be inputted
b: Second input
"""
a: int
b: float
class Outputs(python.Outputs):
"""
Args:
c: Sum of a and b
d: Product of a and b
"""
c: float
d: float
@staticmethod
def function(a, b):
return a + b, a * b
assert issubclass(SampleDef, python.Task)
def test_interface_with_class_no_auto_attribs():
@python.define(auto_attribs=False)
class SampleDef(python.Task["SampleDef.Outputs"]):
a: int = python.arg(help="First input to be inputted")
b: float = python.arg(help="Second input")
x: int
class Outputs(python.Outputs):
c: float = python.out(help="Sum of a and b")
d: float = python.out(help="Product of a and b")
y: str
@staticmethod
def function(a, b):
return a + b, a * b
inputs = sorted(task_fields(SampleDef), key=sort_key)
outputs = sorted(task_fields(SampleDef.Outputs), key=sort_key)
assert inputs == [
python.arg(name="a", type=int, help="First input to be inputted"),
python.arg(name="b", type=float, help="Second input"),
python.arg(
name="function",
type=ty.Callable,
hash_eq=True,
default=attrs.fields(SampleDef).function.default,
),
]
assert outputs == [
python.out(name="c", type=float, help="Sum of a and b"),
python.out(name="d", type=float, help="Product of a and b"),
]
assert SampleDef.function.__name__ == "function"
SampleDef(a=1, b=2.0)
SampleDef.Outputs(c=1.0, d=2.0)
with pytest.raises(TypeError):
SampleDef(a=1, b=2.0, x=3)
with pytest.raises(TypeError):
SampleDef.Outputs(c=1.0, d=2.0, y="hello")
def test_interface_invalid_wrapped1():
with pytest.raises(ValueError):
@python.define(inputs={"a": python.arg()})
class SampleDef(python.Task["SampleDef.Outputs"]):
a: int
class Outputs:
b: float
@staticmethod
def function(a):
return a + 1
def test_interface_invalid_wrapped2():
with pytest.raises(ValueError):
@python.define(outputs={"b": python.out()})
class SampleDef(python.Task["SampleDef.Outputs"]):
a: int
class Outputs:
b: float
@staticmethod
def function(a):
return a + 1
def test_task_repr():
@python.define(outputs=["x", "y", "z"])
def IdentityN3(x: int, y: int = 1, z: int = 2) -> tuple[int, int, int]:
return x, y, z
assert repr(IdentityN3(x=1, y=2)) == "IdentityN3(x=1, y=2)"
@attrs.define(auto_attribs=True)
class A:
x: int
def test_object_input():
"""Test function tasks with object inputs"""
@python.define
def TestFunc(a: A):
return a.x
outputs = TestFunc(a=A(x=7))()
assert outputs.out == 7
def test_no_outputs1():
"""Test function tasks with no outputs specified by None return type"""
@python.define
def TestFunc1(a: A) -> None:
print(a)
outputs = TestFunc1(a=A(x=7))()
assert not task_fields(outputs)
def test_no_outputs2():
"""Test function tasks with no outputs set explicitly"""
@python.define(outputs=[])
def TestFunc2(a: A):
print(a)
outputs = TestFunc2(a=A(x=7))()
assert not task_fields(outputs)
def test_no_outputs_fail():
"""Test function tasks with object inputs"""
@python.define(outputs=[])
def TestFunc3(a: A):
return a
with pytest.raises(ValueError, match="No output fields were specified"):
TestFunc3(a=A(x=7))()
def test_only_one_output_fail():
with pytest.raises(ValueError, match="Multiple outputs specified"):
@python.define(outputs=["out1", "out2"])
def TestFunc4(a: A) -> A:
return a
def test_incorrect_num_outputs_fail():
with pytest.raises(ValueError, match="Length of the outputs"):
@python.define(outputs=["out1", "out2"])
def TestFunc5(a: A) -> tuple[A, A, A]:
return a, a, a