-
Notifications
You must be signed in to change notification settings - Fork 13.7k
/
test_python.py
819 lines (629 loc) · 26.2 KB
/
test_python.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
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import sys
from collections import namedtuple
from datetime import date, timedelta
from typing import Dict # noqa: F401 # This is used by annotation tests.
from typing import Tuple
import pytest
from airflow.decorators import task as task_decorator
from airflow.decorators.base import DecoratedMappedOperator
from airflow.exceptions import AirflowException
from airflow.models import DAG
from airflow.models.baseoperator import BaseOperator
from airflow.models.expandinput import DictOfListsExpandInput
from airflow.models.taskinstance import TaskInstance
from airflow.models.taskmap import TaskMap
from airflow.models.xcom import XCOM_RETURN_KEY
from airflow.models.xcom_arg import PlainXComArg, XComArg
from airflow.utils import timezone
from airflow.utils.state import State
from airflow.utils.task_group import TaskGroup
from airflow.utils.types import DagRunType
from tests.operators.test_python import Call, assert_calls_equal, build_recording_function
from tests.test_utils.db import clear_db_runs
DEFAULT_DATE = timezone.datetime(2016, 1, 1)
END_DATE = timezone.datetime(2016, 1, 2)
INTERVAL = timedelta(hours=12)
FROZEN_NOW = timezone.datetime(2016, 1, 2, 12, 1, 1)
TI_CONTEXT_ENV_VARS = [
'AIRFLOW_CTX_DAG_ID',
'AIRFLOW_CTX_TASK_ID',
'AIRFLOW_CTX_EXECUTION_DATE',
'AIRFLOW_CTX_DAG_RUN_ID',
]
class TestAirflowTaskDecorator:
def setup_class(self):
clear_db_runs()
def setup_method(self):
self.dag = DAG("test_dag", default_args={"owner": "airflow", "start_date": DEFAULT_DATE})
self.run = False
def teardown_method(self):
self.dag.clear()
self.run = False
clear_db_runs()
def test_python_operator_python_callable_is_callable(self):
"""Tests that @task will only instantiate if
the python_callable argument is callable."""
not_callable = {}
with pytest.raises(TypeError):
task_decorator(not_callable, dag=self.dag)
@pytest.mark.parametrize(
"resolve",
[
pytest.param(eval, id="eval"),
pytest.param(lambda t: t, id="stringify"),
],
)
@pytest.mark.parametrize(
"annotation",
[
"dict",
pytest.param(
"dict[str, int]",
marks=pytest.mark.skipif(
sys.version_info < (3, 9),
reason="PEP 585 is implemented in Python 3.9",
),
),
"Dict",
"Dict[str, int]",
],
)
def test_infer_multiple_outputs_using_dict_typing(self, resolve, annotation):
@task_decorator
def identity_dict(x: int, y: int) -> resolve(annotation):
return {"x": x, "y": y}
assert identity_dict(5, 5).operator.multiple_outputs is True
# Check invoking ``@task_decorator.__call__()`` yields the correct inference.
@task_decorator()
def identity_dict_with_decorator_call(x: int, y: int) -> resolve(annotation):
return {"x": x, "y": y}
assert identity_dict_with_decorator_call(5, 5).operator.multiple_outputs is True
def test_infer_multiple_outputs_using_other_typing(self):
@task_decorator
def identity_tuple(x: int, y: int) -> Tuple[int, int]:
return x, y
assert identity_tuple(5, 5).operator.multiple_outputs is False
@task_decorator
def identity_int(x: int) -> int:
return x
assert identity_int(5).operator.multiple_outputs is False
@task_decorator
def identity_notyping(x: int):
return x
assert identity_notyping(5).operator.multiple_outputs is False
# The following cases ensure invoking ``@task_decorator.__call__()`` yields the correct inference.
@task_decorator()
def identity_tuple_with_decorator_call(x: int, y: int) -> Tuple[int, int]:
return x, y
assert identity_tuple_with_decorator_call(5, 5).operator.multiple_outputs is False
@task_decorator()
def identity_int_with_decorator_call(x: int) -> int:
return x
assert identity_int_with_decorator_call(5).operator.multiple_outputs is False
@task_decorator()
def identity_notyping_with_decorator_call(x: int):
return x
assert identity_notyping_with_decorator_call(5).operator.multiple_outputs is False
def test_manual_multiple_outputs_false_with_typings(self):
@task_decorator(multiple_outputs=False)
def identity2(x: int, y: int) -> Tuple[int, int]:
return x, y
with self.dag:
res = identity2(8, 4)
dr = self.dag.create_dagrun(
run_id=DagRunType.MANUAL.value,
start_date=timezone.utcnow(),
execution_date=DEFAULT_DATE,
state=State.RUNNING,
)
res.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
ti = dr.get_task_instances()[0]
assert res.operator.multiple_outputs is False
assert ti.xcom_pull() == [8, 4]
assert ti.xcom_pull(key="return_value_0") is None
assert ti.xcom_pull(key="return_value_1") is None
def test_multiple_outputs_ignore_typing(self):
@task_decorator
def identity_tuple(x: int, y: int) -> Tuple[int, int]:
return x, y
with self.dag:
ident = identity_tuple(35, 36)
dr = self.dag.create_dagrun(
run_id=DagRunType.MANUAL.value,
start_date=timezone.utcnow(),
execution_date=DEFAULT_DATE,
state=State.RUNNING,
)
ident.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
ti = dr.get_task_instances()[0]
assert not ident.operator.multiple_outputs
assert ti.xcom_pull() == [35, 36]
assert ti.xcom_pull(key="return_value_0") is None
assert ti.xcom_pull(key="return_value_1") is None
def test_fails_bad_signature(self):
"""Tests that @task will fail if signature is not binding."""
@task_decorator
def add_number(num: int) -> int:
return num + 2
with pytest.raises(TypeError):
add_number(2, 3)
with pytest.raises(TypeError):
add_number()
add_number('test')
def test_fail_method(self):
"""Tests that @task will fail if signature is not binding."""
with pytest.raises(TypeError):
class Test:
num = 2
@task_decorator
def add_number(self, num: int) -> int:
return self.num + num
def test_fail_multiple_outputs_key_type(self):
@task_decorator(multiple_outputs=True)
def add_number(num: int):
return {2: num}
with self.dag:
ret = add_number(2)
self.dag.create_dagrun(
run_id=DagRunType.MANUAL,
execution_date=DEFAULT_DATE,
start_date=DEFAULT_DATE,
state=State.RUNNING,
)
with pytest.raises(AirflowException):
ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
def test_fail_multiple_outputs_no_dict(self):
@task_decorator(multiple_outputs=True)
def add_number(num: int):
return num
with self.dag:
ret = add_number(2)
self.dag.create_dagrun(
run_id=DagRunType.MANUAL,
execution_date=DEFAULT_DATE,
start_date=DEFAULT_DATE,
state=State.RUNNING,
)
with pytest.raises(AirflowException):
ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
def test_python_callable_arguments_are_templatized(self):
"""Test @task op_args are templatized"""
recorded_calls = []
# Create a named tuple and ensure it is still preserved
# after the rendering is done
Named = namedtuple('Named', ['var1', 'var2'])
named_tuple = Named('{{ ds }}', 'unchanged')
task = task_decorator(
# a Mock instance cannot be used as a callable function or test fails with a
# TypeError: Object of type Mock is not JSON serializable
build_recording_function(recorded_calls),
dag=self.dag,
)
ret = task(4, date(2019, 1, 1), "dag {{dag.dag_id}} ran on {{ds}}.", named_tuple)
self.dag.create_dagrun(
run_id=DagRunType.MANUAL,
execution_date=DEFAULT_DATE,
data_interval=(DEFAULT_DATE, DEFAULT_DATE),
start_date=DEFAULT_DATE,
state=State.RUNNING,
)
ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
ds_templated = DEFAULT_DATE.date().isoformat()
assert len(recorded_calls) == 1
assert_calls_equal(
recorded_calls[0],
Call(
4,
date(2019, 1, 1),
f"dag {self.dag.dag_id} ran on {ds_templated}.",
Named(ds_templated, 'unchanged'),
),
)
def test_python_callable_keyword_arguments_are_templatized(self):
"""Test PythonOperator op_kwargs are templatized"""
recorded_calls = []
task = task_decorator(
# a Mock instance cannot be used as a callable function or test fails with a
# TypeError: Object of type Mock is not JSON serializable
build_recording_function(recorded_calls),
dag=self.dag,
)
ret = task(an_int=4, a_date=date(2019, 1, 1), a_templated_string="dag {{dag.dag_id}} ran on {{ds}}.")
self.dag.create_dagrun(
run_id=DagRunType.MANUAL,
execution_date=DEFAULT_DATE,
data_interval=(DEFAULT_DATE, DEFAULT_DATE),
start_date=DEFAULT_DATE,
state=State.RUNNING,
)
ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
assert len(recorded_calls) == 1
assert_calls_equal(
recorded_calls[0],
Call(
an_int=4,
a_date=date(2019, 1, 1),
a_templated_string=f"dag {self.dag.dag_id} ran on {DEFAULT_DATE.date().isoformat()}.",
),
)
def test_manual_task_id(self):
"""Test manually setting task_id"""
@task_decorator(task_id='some_name')
def do_run():
return 4
with self.dag:
do_run()
assert ['some_name'] == self.dag.task_ids
def test_multiple_calls(self):
"""Test calling task multiple times in a DAG"""
@task_decorator
def do_run():
return 4
with self.dag:
do_run()
assert ['do_run'] == self.dag.task_ids
do_run_1 = do_run()
do_run_2 = do_run()
assert ['do_run', 'do_run__1', 'do_run__2'] == self.dag.task_ids
assert do_run_1.operator.task_id == 'do_run__1'
assert do_run_2.operator.task_id == 'do_run__2'
def test_multiple_calls_in_task_group(self):
"""Test calling task multiple times in a TaskGroup"""
@task_decorator
def do_run():
return 4
group_id = "KnightsOfNii"
with self.dag:
with TaskGroup(group_id=group_id):
do_run()
assert [f"{group_id}.do_run"] == self.dag.task_ids
do_run()
assert [f"{group_id}.do_run", f"{group_id}.do_run__1"] == self.dag.task_ids
assert len(self.dag.task_ids) == 2
def test_call_20(self):
"""Test calling decorated function 21 times in a DAG"""
@task_decorator
def __do_run():
return 4
with self.dag:
__do_run()
for _ in range(20):
__do_run()
assert self.dag.task_ids[-1] == '__do_run__20'
def test_multiple_outputs(self):
"""Tests pushing multiple outputs as a dictionary"""
@task_decorator(multiple_outputs=True)
def return_dict(number: int):
return {'number': number + 1, '43': 43}
test_number = 10
with self.dag:
ret = return_dict(test_number)
dr = self.dag.create_dagrun(
run_id=DagRunType.MANUAL,
start_date=timezone.utcnow(),
execution_date=DEFAULT_DATE,
state=State.RUNNING,
)
ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
ti = dr.get_task_instances()[0]
assert ti.xcom_pull(key='number') == test_number + 1
assert ti.xcom_pull(key='43') == 43
assert ti.xcom_pull() == {'number': test_number + 1, '43': 43}
def test_default_args(self):
"""Test that default_args are captured when calling the function correctly"""
@task_decorator
def do_run():
return 4
with self.dag:
ret = do_run()
assert ret.operator.owner == 'airflow'
@task_decorator
def test_apply_default_raise(unknown):
return unknown
with pytest.raises(TypeError):
with self.dag:
test_apply_default_raise()
@task_decorator
def test_apply_default(owner):
return owner
with self.dag:
ret = test_apply_default()
assert 'owner' in ret.operator.op_kwargs
def test_xcom_arg(self):
"""Tests that returned key in XComArg is returned correctly"""
@task_decorator
def add_2(number: int):
return number + 2
@task_decorator
def add_num(number: int, num2: int = 2):
return number + num2
test_number = 10
with self.dag:
bigger_number = add_2(test_number)
ret = add_num(bigger_number, XComArg(bigger_number.operator))
dr = self.dag.create_dagrun(
run_id=DagRunType.MANUAL,
start_date=timezone.utcnow(),
execution_date=DEFAULT_DATE,
state=State.RUNNING,
)
bigger_number.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
ret.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
ti_add_num = [ti for ti in dr.get_task_instances() if ti.task_id == 'add_num'][0]
assert ti_add_num.xcom_pull(key=ret.key) == (test_number + 2) * 2
def test_dag_task(self):
"""Tests dag.task property to generate task"""
@self.dag.task
def add_2(number: int):
return number + 2
test_number = 10
res = add_2(test_number)
add_2(res)
assert 'add_2' in self.dag.task_ids
def test_dag_task_multiple_outputs(self):
"""Tests dag.task property to generate task with multiple outputs"""
@self.dag.task(multiple_outputs=True)
def add_2(number: int):
return {'1': number + 2, '2': 42}
test_number = 10
add_2(test_number)
add_2(test_number)
assert 'add_2' in self.dag.task_ids
def test_task_documentation(self):
"""Tests that task_decorator loads doc_md from function doc"""
@task_decorator
def add_2(number: int):
"""
Adds 2 to number.
"""
return number + 2
test_number = 10
with self.dag:
ret = add_2(test_number)
assert ret.operator.doc_md.strip(), "Adds 2 to number."
def test_user_provided_task_id_in_a_loop_is_used(self):
"""Tests that when looping that user provided task_id is used"""
@task_decorator(task_id='hello_task')
def hello():
"""
Print Hello world
"""
print("Hello world")
with self.dag:
for i in range(3):
hello.override(task_id=f'my_task_id_{i * 2}')()
hello() # This task would have hello_task as the task_id
assert self.dag.task_ids == ['my_task_id_0', 'my_task_id_2', 'my_task_id_4', 'hello_task']
def test_user_provided_pool_and_priority_weight_works(self):
"""Tests that when looping that user provided pool, priority_weight etc is used"""
@task_decorator(task_id='hello_task')
def hello():
"""
Print Hello world
"""
print("Hello world")
with self.dag:
for i in range(3):
hello.override(pool='my_pool', priority_weight=i)()
weights = []
for task in self.dag.tasks:
assert task.pool == 'my_pool'
weights.append(task.priority_weight)
assert weights == [0, 1, 2]
def test_python_callable_args_work_as_well_as_baseoperator_args(self):
"""Tests that when looping that user provided pool, priority_weight etc is used"""
@task_decorator(task_id='hello_task')
def hello(x, y):
"""
Print Hello world
"""
print("Hello world", x, y)
return x, y
with self.dag:
output = hello.override(task_id='mytask')(x=2, y=3)
output2 = hello.override()(2, 3) # nothing overridden but should work
assert output.operator.op_kwargs == {'x': 2, 'y': 3}
assert output2.operator.op_args == (2, 3)
output.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
output2.operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
def test_mapped_decorator_shadow_context() -> None:
@task_decorator
def print_info(message: str, run_id: str = "") -> None:
print(f"{run_id}: {message}")
with pytest.raises(ValueError) as ctx:
print_info.partial(run_id="hi")
assert str(ctx.value) == "cannot call partial() on task context variable 'run_id'"
with pytest.raises(ValueError) as ctx:
print_info.expand(run_id=["hi", "there"])
assert str(ctx.value) == "cannot call expand() on task context variable 'run_id'"
def test_mapped_decorator_wrong_argument() -> None:
@task_decorator
def print_info(message: str, run_id: str = "") -> None:
print(f"{run_id}: {message}")
with pytest.raises(TypeError) as ct:
print_info.partial(wrong_name="hi")
assert str(ct.value) == "partial() got an unexpected keyword argument 'wrong_name'"
with pytest.raises(TypeError) as ct:
print_info.expand(wrong_name=["hi", "there"])
assert str(ct.value) == "expand() got an unexpected keyword argument 'wrong_name'"
with pytest.raises(ValueError) as cv:
print_info.expand(message="hi")
assert str(cv.value) == "expand() got an unexpected type 'str' for keyword argument 'message'"
def test_mapped_decorator():
@task_decorator
def print_info(m1: str, m2: str, run_id: str = "") -> None:
print(f"{run_id}: {m1} {m2}")
@task_decorator
def print_everything(**kwargs) -> None:
print(kwargs)
with DAG("test_mapped_decorator", start_date=DEFAULT_DATE):
t0 = print_info.expand(m1=["a", "b"], m2={"foo": "bar"})
t1 = print_info.partial(m1="hi").expand(m2=[1, 2, 3])
t2 = print_everything.partial(whatever="123").expand(any_key=[1, 2], works=t1)
assert isinstance(t2, XComArg)
assert isinstance(t2.operator, DecoratedMappedOperator)
assert t2.operator.task_id == "print_everything"
assert t2.operator.op_kwargs_expand_input == DictOfListsExpandInput({"any_key": [1, 2], "works": t1})
assert t0.operator.task_id == "print_info"
assert t1.operator.task_id == "print_info__1"
def test_mapped_decorator_invalid_args() -> None:
@task_decorator
def double(number: int):
return number * 2
literal = [1, 2, 3]
with pytest.raises(TypeError, match="arguments 'other', 'b'"):
double.partial(other=[1], b=['a'])
with pytest.raises(TypeError, match="argument 'other'"):
double.expand(number=literal, other=[1])
with pytest.raises(ValueError, match="argument 'number'"):
double.expand(number=1) # type: ignore[arg-type]
def test_partial_mapped_decorator() -> None:
@task_decorator
def product(number: int, multiple: int):
return number * multiple
literal = [1, 2, 3]
with DAG('test_dag', start_date=DEFAULT_DATE) as dag:
quadrupled = product.partial(multiple=3).expand(number=literal)
doubled = product.partial(multiple=2).expand(number=literal)
trippled = product.partial(multiple=3).expand(number=literal)
product.partial(multiple=2) # No operator is actually created.
assert isinstance(doubled, PlainXComArg)
assert isinstance(trippled, PlainXComArg)
assert isinstance(quadrupled, PlainXComArg)
assert dag.task_dict == {
"product": quadrupled.operator,
"product__1": doubled.operator,
"product__2": trippled.operator,
}
assert isinstance(doubled.operator, DecoratedMappedOperator)
assert doubled.operator.op_kwargs_expand_input == DictOfListsExpandInput({"number": literal})
assert doubled.operator.partial_kwargs["op_kwargs"] == {"multiple": 2}
assert isinstance(trippled.operator, DecoratedMappedOperator) # For type-checking on partial_kwargs.
assert trippled.operator.partial_kwargs["op_kwargs"] == {"multiple": 3}
assert doubled.operator is not trippled.operator
def test_mapped_decorator_unmap_merge_op_kwargs(dag_maker, session):
with dag_maker(session=session):
@task_decorator
def task1():
return ["x"]
@task_decorator
def task2(arg1, arg2):
...
task2.partial(arg1=1).expand(arg2=task1())
run = dag_maker.create_dagrun()
# Run task1.
dec = run.task_instance_scheduling_decisions(session=session)
assert [ti.task_id for ti in dec.schedulable_tis] == ["task1"]
dec.schedulable_tis[0].run(session=session)
# Expand task2.
dec = run.task_instance_scheduling_decisions(session=session)
assert [ti.task_id for ti in dec.schedulable_tis] == ["task2"]
ti = dec.schedulable_tis[0]
unmapped = ti.task.unmap((ti.get_template_context(session), session))
assert set(unmapped.op_kwargs) == {"arg1", "arg2"}
def test_mapped_decorator_converts_partial_kwargs(dag_maker, session):
with dag_maker(session=session):
@task_decorator
def task1(arg):
return ["x" * arg]
@task_decorator(retry_delay=30)
def task2(arg1, arg2):
...
task2.partial(arg1=1).expand(arg2=task1.expand(arg=[1, 2]))
run = dag_maker.create_dagrun()
# Expand and run task1.
dec = run.task_instance_scheduling_decisions(session=session)
assert [ti.task_id for ti in dec.schedulable_tis] == ["task1", "task1"]
for ti in dec.schedulable_tis:
ti.run(session=session)
assert not ti.task.is_mapped
assert ti.task.retry_delay == timedelta(seconds=300) # Operator default.
# Expand task2.
dec = run.task_instance_scheduling_decisions(session=session)
assert [ti.task_id for ti in dec.schedulable_tis] == ["task2", "task2"]
for ti in dec.schedulable_tis:
unmapped = ti.task.unmap((ti.get_template_context(session), session))
assert unmapped.retry_delay == timedelta(seconds=30)
def test_mapped_render_template_fields(dag_maker, session):
@task_decorator
def fn(arg1, arg2):
...
with dag_maker(session=session):
task1 = BaseOperator(task_id="op1")
mapped = fn.partial(arg2='{{ ti.task_id }}').expand(arg1=task1.output)
dr = dag_maker.create_dagrun()
ti: TaskInstance = dr.get_task_instance(task1.task_id, session=session)
ti.xcom_push(key=XCOM_RETURN_KEY, value=['{{ ds }}'], session=session)
session.add(
TaskMap(
dag_id=dr.dag_id,
task_id=task1.task_id,
run_id=dr.run_id,
map_index=-1,
length=1,
keys=None,
)
)
session.flush()
mapped_ti: TaskInstance = dr.get_task_instance(mapped.operator.task_id, session=session)
mapped_ti.map_index = 0
assert mapped_ti.task.is_mapped
mapped.operator.render_template_fields(context=mapped_ti.get_template_context(session=session))
assert not mapped_ti.task.is_mapped
assert mapped_ti.task.op_kwargs['arg1'] == "{{ ds }}"
assert mapped_ti.task.op_kwargs['arg2'] == "fn"
def test_task_decorator_has_wrapped_attr():
"""
Test @task original underlying function is accessible
through the __wrapped__ attribute.
"""
def org_test_func():
pass
decorated_test_func = task_decorator(org_test_func)
assert hasattr(
decorated_test_func, '__wrapped__'
), "decorated function does not have __wrapped__ attribute"
assert decorated_test_func.__wrapped__ is org_test_func, "__wrapped__ attr is not the original function"
def test_upstream_exception_produces_none_xcom(dag_maker, session):
from airflow.exceptions import AirflowSkipException
from airflow.models.dagrun import DagRun
from airflow.utils.trigger_rule import TriggerRule
result = None
with dag_maker(session=session) as dag:
@dag.task()
def up1() -> str:
return "example"
@dag.task()
def up2() -> None:
raise AirflowSkipException()
@dag.task(trigger_rule=TriggerRule.NONE_FAILED_MIN_ONE_SUCCESS)
def down(a, b):
nonlocal result
result = f"{a!r} {b!r}"
down(up1(), up2())
dr: DagRun = dag_maker.create_dagrun()
decision = dr.task_instance_scheduling_decisions(session=session)
assert len(decision.schedulable_tis) == 2 # "up1" and "up2"
for ti in decision.schedulable_tis:
ti.run(session=session)
decision = dr.task_instance_scheduling_decisions(session=session)
assert len(decision.schedulable_tis) == 1 # "down"
decision.schedulable_tis[0].run(session=session)
assert result == "'example' None"