|
| 1 | +use ruff_diagnostics::{Diagnostic, Violation}; |
| 2 | +use ruff_macros::{derive_message_formats, violation}; |
| 3 | +use ruff_python_ast::Expr; |
| 4 | +use ruff_text_size::Ranged; |
| 5 | + |
| 6 | +use crate::checkers::ast::Checker; |
| 7 | + |
| 8 | +#[derive(Debug, Eq, PartialEq)] |
| 9 | +enum Replacement { |
| 10 | + None, |
| 11 | + Name(String), |
| 12 | +} |
| 13 | + |
| 14 | +impl Replacement { |
| 15 | + fn name(name: impl Into<String>) -> Self { |
| 16 | + Self::Name(name.into()) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +/// ## What it does |
| 21 | +/// Checks for uses of deprecated Airflow functions and values. |
| 22 | +/// |
| 23 | +/// ## Why is this bad? |
| 24 | +/// Airflow 3.0 removed various deprecated functions, members, and other |
| 25 | +/// values. Some have more modern replacements. Others are considered too niche |
| 26 | +/// and not worth to be maintained in Airflow. |
| 27 | +/// |
| 28 | +/// ## Example |
| 29 | +/// ```python |
| 30 | +/// from airflow.utils.dates import days_ago |
| 31 | +/// |
| 32 | +/// |
| 33 | +/// yesterday = days_ago(today, 1) |
| 34 | +/// ``` |
| 35 | +/// |
| 36 | +/// Use instead: |
| 37 | +/// ```python |
| 38 | +/// from datetime import timedelta |
| 39 | +/// |
| 40 | +/// |
| 41 | +/// yesterday = today - timedelta(days=1) |
| 42 | +/// ``` |
| 43 | +#[violation] |
| 44 | +pub struct AirflowDeprecatedMembers { |
| 45 | + deprecated: String, |
| 46 | + replacement: Replacement, |
| 47 | +} |
| 48 | + |
| 49 | +impl Violation for AirflowDeprecatedMembers { |
| 50 | + #[derive_message_formats] |
| 51 | + fn message(&self) -> String { |
| 52 | + let AirflowDeprecatedMembers { |
| 53 | + deprecated, |
| 54 | + replacement, |
| 55 | + } = self; |
| 56 | + match replacement { |
| 57 | + Replacement::None => format!("`{deprecated}` is removed in Airflow 3.0"), |
| 58 | + Replacement::Name(name) => { |
| 59 | + format!("`{deprecated}` is removed in Airflow 3.0; use {name} instead") |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// AIR302 |
| 66 | +pub(crate) fn deprecated_members(checker: &mut Checker, expr: &Expr) { |
| 67 | + let Some((deprecated, replacement)) = |
| 68 | + checker |
| 69 | + .semantic() |
| 70 | + .resolve_qualified_name(expr) |
| 71 | + .and_then(|qualname| match qualname.segments() { |
| 72 | + ["airflow", "utils", "dates", "date_range"] => { |
| 73 | + Some((qualname.to_string(), Replacement::None)) |
| 74 | + } |
| 75 | + ["airflow", "utils", "dates", "days_ago"] => Some(( |
| 76 | + qualname.to_string(), |
| 77 | + Replacement::name("datetime.timedelta()"), |
| 78 | + )), |
| 79 | + _ => None, |
| 80 | + }) |
| 81 | + else { |
| 82 | + return; |
| 83 | + }; |
| 84 | + |
| 85 | + checker.diagnostics.push(Diagnostic::new( |
| 86 | + AirflowDeprecatedMembers { |
| 87 | + deprecated, |
| 88 | + replacement, |
| 89 | + }, |
| 90 | + expr.range(), |
| 91 | + )); |
| 92 | +} |
0 commit comments