-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[airflow] rule for deprecated task_concurrency parameter (AIR303) #14616
Open
abhishekbhakat
wants to merge
1
commit into
astral-sh:main
Choose a base branch
from
abhishekbhakat:deprecated-task-concurrency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+129
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
crates/ruff_linter/resources/test/fixtures/airflow/AIR303.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from airflow.operators.python import PythonOperator | ||
from airflow import DAG | ||
|
||
# Using deprecated task_concurrency parameter | ||
task1 = PythonOperator( | ||
task_id="task1", | ||
task_concurrency=2, # This should trigger AIR303 | ||
python_callable=lambda: None, | ||
) | ||
|
||
# Using new max_active_tis_per_dag parameter | ||
task2 = PythonOperator( | ||
task_id="task2", | ||
max_active_tis_per_dag=2, # This should be fine | ||
python_callable=lambda: None, | ||
) | ||
|
||
# Another example with task_concurrency | ||
task3 = PythonOperator( | ||
task_id="task3", | ||
task_concurrency=5, # This should trigger AIR303 | ||
python_callable=lambda: None, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub(crate) use dag_schedule_argument::*; | ||
pub(crate) use task_concurrency::*; | ||
pub(crate) use task_variable_name::*; | ||
|
||
mod dag_schedule_argument; | ||
mod task_concurrency; | ||
mod task_variable_name; |
75 changes: 75 additions & 0 deletions
75
crates/ruff_linter/src/rules/airflow/rules/task_concurrency.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast as ast; | ||
use ruff_python_ast::Expr; | ||
use ruff_python_semantic::Modules; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for usage of the deprecated `task_concurrency` parameter in Airflow Operators. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `task_concurrency` parameter has been deprecated and renamed to `max_active_tis_per_dag` | ||
/// in Airflow 3.0. Code using the old parameter name needs to be updated to ensure | ||
/// compatibility with Airflow 3.0. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from airflow.operators.python import PythonOperator | ||
/// | ||
/// # Invalid: using deprecated parameter | ||
/// task = PythonOperator(task_id="my_task", task_concurrency=2) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from airflow.operators.python import PythonOperator | ||
/// | ||
/// # Valid: using new parameter name | ||
/// task = PythonOperator(task_id="my_task", max_active_tis_per_dag=2) | ||
/// ``` | ||
#[violation] | ||
pub struct AirflowDeprecatedTaskConcurrency { | ||
pub old_param: String, | ||
pub new_param: String, | ||
} | ||
|
||
impl Violation for AirflowDeprecatedTaskConcurrency { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let AirflowDeprecatedTaskConcurrency { | ||
old_param, | ||
new_param, | ||
} = self; | ||
format!("Use `{new_param}` instead of deprecated `{old_param}` parameter") | ||
} | ||
} | ||
|
||
/// AIR303 | ||
pub(crate) fn task_concurrency_check(checker: &mut Checker, value: &Expr) { | ||
// If the value is not a call, we can't do anything. | ||
let Expr::Call(ast::ExprCall { | ||
func: _, arguments, .. | ||
}) = value | ||
else { | ||
return; | ||
}; | ||
|
||
// If we haven't seen any airflow imports, we can't do anything. | ||
if !checker.semantic().seen_module(Modules::AIRFLOW) { | ||
return; | ||
} | ||
|
||
// Check for the deprecated parameter | ||
if let Some(keyword) = arguments.find_keyword("task_concurrency") { | ||
checker.diagnostics.push(Diagnostic::new( | ||
AirflowDeprecatedTaskConcurrency { | ||
old_param: "task_concurrency".to_string(), | ||
new_param: "max_active_tis_per_dag".to_string(), | ||
}, | ||
keyword.range(), | ||
)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ter/src/rules/airflow/snapshots/ruff_linter__rules__airflow__tests__AIR303_AIR303.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/airflow/mod.rs | ||
snapshot_kind: text | ||
--- | ||
AIR303.py:7:5: AIR303 Use `max_active_tis_per_dag` instead of deprecated `task_concurrency` parameter | ||
| | ||
5 | task1 = PythonOperator( | ||
6 | task_id="task1", | ||
7 | task_concurrency=2, # This should trigger AIR303 | ||
| ^^^^^^^^^^^^^^^^^^ AIR303 | ||
8 | python_callable=lambda: None, | ||
9 | ) | ||
| | ||
|
||
AIR303.py:21:5: AIR303 Use `max_active_tis_per_dag` instead of deprecated `task_concurrency` parameter | ||
| | ||
19 | task3 = PythonOperator( | ||
20 | task_id="task3", | ||
21 | task_concurrency=5, # This should trigger AIR303 | ||
| ^^^^^^^^^^^^^^^^^^ AIR303 | ||
22 | python_callable=lambda: None, | ||
23 | ) | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abhishekbhakat We should raise a deprecation error in Airflow 2.2+ too: apache/airflow#17708 (that was when we had original deprecation)
In Airflow 3, it is "removed" entirely.