|
| 1 | +from airflow.decorators import task |
| 2 | +from airflow.operators.python import BranchPythonOperator, ShortCircuitOperator |
| 3 | +from airflow.providers.standard.operators.python import ( |
| 4 | + BranchPythonOperator as ProviderBranchPythonOperator, |
| 5 | +) |
| 6 | + |
| 7 | +condition1 = True |
| 8 | +condition2 = True |
| 9 | +condition3 = True |
| 10 | + |
| 11 | + |
| 12 | +# Violations (should trigger AIR004): |
| 13 | + |
| 14 | +@task.branch |
| 15 | +def two_returns_one_non_empty(): # AIR004 |
| 16 | + if condition1: |
| 17 | + return ["my_downstream_task"] |
| 18 | + return [] |
| 19 | + |
| 20 | + |
| 21 | +@task.branch |
| 22 | +def three_returns_one_non_empty(): # AIR004 |
| 23 | + if condition1: |
| 24 | + return [] |
| 25 | + if condition2: |
| 26 | + return ["another_downstream_task"] |
| 27 | + return [] |
| 28 | + |
| 29 | + |
| 30 | +@task.branch |
| 31 | +def nested_returns_one_non_empty(): # AIR004 |
| 32 | + if condition1: |
| 33 | + if condition2: |
| 34 | + return [] |
| 35 | + return ["downstream_task"] |
| 36 | + return [] |
| 37 | + |
| 38 | + |
| 39 | +@task.branch() |
| 40 | +def with_parens(): # AIR004 |
| 41 | + if condition1: |
| 42 | + return ["downstream_task"] |
| 43 | + return [] |
| 44 | + |
| 45 | + |
| 46 | +@task.branch |
| 47 | +def bare_return_and_list(): # AIR004 |
| 48 | + if condition1: |
| 49 | + return ["downstream_task"] |
| 50 | + return |
| 51 | + |
| 52 | + |
| 53 | +@task.branch |
| 54 | +def none_return_and_list(): # AIR004 |
| 55 | + if condition1: |
| 56 | + return ["downstream_task"] |
| 57 | + return None |
| 58 | + |
| 59 | + |
| 60 | +# No violations: |
| 61 | + |
| 62 | +@task.branch |
| 63 | +def two_non_empty_returns(): |
| 64 | + if condition1: |
| 65 | + return ["task_a"] |
| 66 | + if condition2: |
| 67 | + return ["task_b"] |
| 68 | + return [] |
| 69 | + |
| 70 | + |
| 71 | +@task.branch |
| 72 | +def all_empty_returns(): |
| 73 | + if condition1: |
| 74 | + return [] |
| 75 | + if condition2: |
| 76 | + return [] |
| 77 | + return [] |
| 78 | + |
| 79 | + |
| 80 | +@task.branch |
| 81 | +def single_return(): |
| 82 | + return ["downstream_task"] |
| 83 | + |
| 84 | + |
| 85 | +def not_decorated(): |
| 86 | + if condition1: |
| 87 | + return ["downstream_task"] |
| 88 | + return [] |
| 89 | + |
| 90 | + |
| 91 | +@task.short_circuit |
| 92 | +def already_short_circuit(): |
| 93 | + if condition1: |
| 94 | + return True |
| 95 | + return False |
| 96 | + |
| 97 | + |
| 98 | +# BranchPythonOperator violations (should trigger AIR004): |
| 99 | + |
| 100 | + |
| 101 | +def operator_short_circuit_candidate(): |
| 102 | + if condition1: |
| 103 | + return ["downstream_task"] |
| 104 | + return [] |
| 105 | + |
| 106 | + |
| 107 | +BranchPythonOperator(task_id="task", python_callable=operator_short_circuit_candidate) # AIR004 |
| 108 | + |
| 109 | + |
| 110 | +def provider_short_circuit_candidate(): |
| 111 | + if condition1: |
| 112 | + return ["downstream_task"] |
| 113 | + return [] |
| 114 | + |
| 115 | + |
| 116 | +ProviderBranchPythonOperator( # AIR004 |
| 117 | + task_id="task", python_callable=provider_short_circuit_candidate |
| 118 | +) |
| 119 | + |
| 120 | + |
| 121 | +# BranchPythonOperator non-violations: |
| 122 | + |
| 123 | + |
| 124 | +def operator_two_non_empty(): |
| 125 | + if condition1: |
| 126 | + return ["task_a"] |
| 127 | + if condition2: |
| 128 | + return ["task_b"] |
| 129 | + return [] |
| 130 | + |
| 131 | + |
| 132 | +BranchPythonOperator(task_id="task", python_callable=operator_two_non_empty) |
| 133 | + |
| 134 | + |
| 135 | +def operator_single_return(): |
| 136 | + return ["downstream_task"] |
| 137 | + |
| 138 | + |
| 139 | +BranchPythonOperator(task_id="task", python_callable=operator_single_return) |
| 140 | + |
| 141 | +ShortCircuitOperator(task_id="task", python_callable=operator_short_circuit_candidate) |
| 142 | + |
| 143 | +BranchPythonOperator(task_id="task", python_callable=lambda: ["downstream_task"]) |
| 144 | + |
| 145 | +BranchPythonOperator(task_id="task") |
0 commit comments