[airflow] Flag runtime-varying values in DAG/task constructor arguments (AIR304)#23631
[airflow] Flag runtime-varying values in DAG/task constructor arguments (AIR304)#23631ntBre merged 2 commits intoastral-sh:mainfrom
airflow] Flag runtime-varying values in DAG/task constructor arguments (AIR304)#23631Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| AIR304 | 97 | 97 | 0 | 0 | 0 |
cfc0bce to
0a4d03a
Compare
Lee-W
left a comment
There was a problem hiding this comment.
a few nits, but looks good in general!
crates/ruff_linter/src/rules/airflow/rules/runtime_value_in_dag_or_task.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/airflow/rules/runtime_value_in_dag_or_task.rs
Show resolved
Hide resolved
crates/ruff_linter/src/rules/airflow/rules/runtime_value_in_dag_or_task.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/airflow/rules/runtime_value_in_dag_or_task.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/airflow/rules/runtime_value_in_dag_or_task.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/airflow/rules/runtime_value_in_dag_or_task.rs
Show resolved
Hide resolved
18756ae to
2e8e72c
Compare
ntBre
left a comment
There was a problem hiding this comment.
Thank you! This looks good to me. I just had one suggestion to compare to an existing helper method to see if there are any other cases you want to handle in find_runtime_varying_call.
crates/ruff_linter/src/rules/airflow/rules/runtime_value_in_dag_or_task.rs
Show resolved
Hide resolved
Using runtime-varying values (like `datetime.now()`) as arguments to Airflow DAG or task constructors causes the serialized DAG hash to change on every parse, creating infinite DAG versions in the database. This rule detects such calls in DAG constructors, @dag decorators, operator/sensor constructors, and @task decorators, recursively checking through binary ops, dicts, lists, sets, tuples, and f-strings. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2e8e72c to
c48870d
Compare
airflow] Flag runtime-varying values in DAG/task constructor arguments (AIR304)airflow] Flag runtime-varying values in DAG/task constructor arguments (AIR304)
|
I guess one more quick question before landing: is this behavior specific to Airflow 3? I think so far we've used the |
Good question! Yes, this only affects Airflow3. This is because Dag versioning was introduced in Airflow 3, so these patterns weren't problematic before. |
|
Sounds good, thank you! I'll land this then! |
Summary
Add rule AIR304 that flags runtime-varying function calls (e.g.,
datetime.now(),pendulum.now(),random.randint(),uuid.uuid4()) used as arguments in Airflow DAG/task constructors. These calls cause the serialized DAG hash to change on every parse, creating infinite DAG versions in thedag_versionandserialized_dagtables, leading to unbounded database growth and eventual OOM conditions.Reference: apache/airflow#59430
The rule checks:
DAG(...)constructors and@dag(...)decorator callsis_airflow_builtin_or_provider)@task(...)decorator callsIt recursively inspects keyword argument values through binary/unary ops, dicts, lists, sets, tuples, and f-strings to find calls to known runtime-varying functions from
datetime,pendulum,time,uuid, andrandom.Test Plan
cargo test -p ruff_linter -- airflow::tests— all 43 tests pass, including the new AIR304 test case with 17 violation and 6 non-violation scenarios covering direct calls, binary ops,default_argsdicts, f-strings, operators, sensors, decorators, and non-airflow calls.Related: apache/airflow#43176
CC: @Lee-W @sjyangkevin @wjddn279