-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Airflow rule for 3.0 removals
Airflow 3.0 removes various deprecated functions, members, modules, and other values. They have been deprecated in 2.x, but the removal causes incompatibilities that we want to detect.
- Loading branch information
Showing
7 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
crates/ruff_linter/resources/test/fixtures/airflow/AIR302.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,12 @@ | ||
from airflow.utils import dates | ||
from airflow.utils.dates import date_range, datetime_to_nano, days_ago | ||
|
||
date_range | ||
days_ago | ||
|
||
dates.date_range | ||
dates.days_ago | ||
|
||
# This one was not deprecated. | ||
datetime_to_nano | ||
dates.datetime_to_nano |
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
92 changes: 92 additions & 0 deletions
92
crates/ruff_linter/src/rules/airflow/rules/deprecated_members.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,92 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::Expr; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
enum Replacement { | ||
None, | ||
Name(String), | ||
} | ||
|
||
impl Replacement { | ||
fn name(name: impl Into<String>) -> Self { | ||
Self::Name(name.into()) | ||
} | ||
} | ||
|
||
/// ## What it does | ||
/// Checks for uses of deprecated Airflow functions and values. | ||
/// | ||
/// ## Why is this bad? | ||
/// Airflow 3.0 removed various deprecated functions, members, and other | ||
/// values. Some have more modern replacements. Others are considered too niche | ||
/// and not worth to be maintained in Airflow. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from airflow.utils.dates import days_ago | ||
/// | ||
/// | ||
/// yesterday = days_ago(today, 1) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from datetime import timedelta | ||
/// | ||
/// | ||
/// yesterday = today - timedelta(days=1) | ||
/// ``` | ||
#[violation] | ||
pub struct AirflowDeprecatedMembers { | ||
deprecated: String, | ||
replacement: Replacement, | ||
} | ||
|
||
impl Violation for AirflowDeprecatedMembers { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let AirflowDeprecatedMembers { | ||
deprecated, | ||
replacement, | ||
} = self; | ||
match replacement { | ||
Replacement::None => format!("`{deprecated}` is removed in Airflow 3.0"), | ||
Replacement::Name(name) => { | ||
format!("`{deprecated}` is removed in Airflow 3.0; use {name} instead") | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// AIR302 | ||
pub(crate) fn deprecated_members(checker: &mut Checker, expr: &Expr) { | ||
let Some((deprecated, replacement)) = | ||
checker | ||
.semantic() | ||
.resolve_qualified_name(expr) | ||
.and_then(|qualname| match qualname.segments() { | ||
["airflow", "utils", "dates", "date_range"] => { | ||
Some((qualname.to_string(), Replacement::None)) | ||
} | ||
["airflow", "utils", "dates", "days_ago"] => Some(( | ||
qualname.to_string(), | ||
Replacement::name("datetime.timedelta()"), | ||
)), | ||
_ => None, | ||
}) | ||
else { | ||
return; | ||
}; | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
AirflowDeprecatedMembers { | ||
deprecated, | ||
replacement, | ||
}, | ||
expr.range(), | ||
)); | ||
} |
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,3 +1,5 @@ | ||
pub(crate) use deprecated_members::*; | ||
pub(crate) use task_variable_name::*; | ||
|
||
mod deprecated_members; | ||
mod task_variable_name; |
38 changes: 38 additions & 0 deletions
38
...ter/src/rules/airflow/snapshots/ruff_linter__rules__airflow__tests__AIR302_AIR302.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,38 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/airflow/mod.rs | ||
--- | ||
AIR302.py:4:1: AIR302 `airflow.utils.dates.date_range` is removed in Airflow 3.0 | ||
| | ||
2 | from airflow.utils.dates import date_range, datetime_to_nano, days_ago | ||
3 | | ||
4 | date_range | ||
| ^^^^^^^^^^ AIR302 | ||
5 | days_ago | ||
| | ||
|
||
AIR302.py:5:1: AIR302 `airflow.utils.dates.days_ago` is removed in Airflow 3.0; use datetime.timedelta() instead | ||
| | ||
4 | date_range | ||
5 | days_ago | ||
| ^^^^^^^^ AIR302 | ||
6 | | ||
7 | dates.date_range | ||
| | ||
|
||
AIR302.py:7:1: AIR302 `airflow.utils.dates.date_range` is removed in Airflow 3.0 | ||
| | ||
5 | days_ago | ||
6 | | ||
7 | dates.date_range | ||
| ^^^^^^^^^^^^^^^^ AIR302 | ||
8 | dates.days_ago | ||
| | ||
|
||
AIR302.py:8:1: AIR302 `airflow.utils.dates.days_ago` is removed in Airflow 3.0; use datetime.timedelta() instead | ||
| | ||
7 | dates.date_range | ||
8 | dates.days_ago | ||
| ^^^^^^^^^^^^^^ AIR302 | ||
9 | | ||
10 | # This one was not deprecated. | ||
| |