|
| 1 | +use ruff_diagnostics::{Diagnostic, Violation}; |
| 2 | +use ruff_macros::{derive_message_formats, ViolationMetadata}; |
| 3 | +use ruff_python_ast::{name::QualifiedName, Arguments, Expr, ExprAttribute, ExprCall}; |
| 4 | +use ruff_python_semantic::Modules; |
| 5 | +use ruff_text_size::Ranged; |
| 6 | + |
| 7 | +use crate::checkers::ast::Checker; |
| 8 | + |
| 9 | +#[derive(Debug, Eq, PartialEq)] |
| 10 | +enum Replacement { |
| 11 | + None, |
| 12 | + Name(String), |
| 13 | +} |
| 14 | + |
| 15 | +/// ## What it does |
| 16 | +/// Checks for uses of deprecated Airflow functions and values. |
| 17 | +/// |
| 18 | +/// ## Why is this bad? |
| 19 | +/// Airflow 3.0 removed various deprecated functions, members, and other |
| 20 | +/// values. Some have more modern replacements. Others are considered too niche |
| 21 | +/// and not worth to be maintained in Airflow. |
| 22 | +/// |
| 23 | +/// ## Example |
| 24 | +/// ```python |
| 25 | +/// from airflow.utils.dates import days_ago |
| 26 | +/// |
| 27 | +/// |
| 28 | +/// yesterday = days_ago(today, 1) |
| 29 | +/// ``` |
| 30 | +/// |
| 31 | +/// Use instead: |
| 32 | +/// ```python |
| 33 | +/// from datetime import timedelta |
| 34 | +/// |
| 35 | +/// |
| 36 | +/// yesterday = today - timedelta(days=1) |
| 37 | +/// ``` |
| 38 | +#[derive(ViolationMetadata)] |
| 39 | +pub(crate) struct Airflow3Removal { |
| 40 | + deprecated: String, |
| 41 | + replacement: Replacement, |
| 42 | +} |
| 43 | + |
| 44 | +impl Violation for Airflow3Removal { |
| 45 | + #[derive_message_formats] |
| 46 | + fn message(&self) -> String { |
| 47 | + let Airflow3Removal { |
| 48 | + deprecated, |
| 49 | + replacement, |
| 50 | + } = self; |
| 51 | + match replacement { |
| 52 | + Replacement::None => format!("`{deprecated}` is removed in Airflow 3.0"), |
| 53 | + Replacement::Name(name) => { |
| 54 | + format!("`{deprecated}` is removed in Airflow 3.0; use {name} instead") |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +fn diagnostic_for_argument( |
| 61 | + arguments: &Arguments, |
| 62 | + deprecated: &str, |
| 63 | + replacement: Option<&str>, |
| 64 | +) -> Option<Diagnostic> { |
| 65 | + let keyword = arguments.find_keyword(deprecated)?; |
| 66 | + Some(Diagnostic::new( |
| 67 | + Airflow3Removal { |
| 68 | + deprecated: (*deprecated).to_string(), |
| 69 | + replacement: match replacement { |
| 70 | + Some(name) => Replacement::Name(name.to_owned()), |
| 71 | + None => Replacement::None, |
| 72 | + }, |
| 73 | + }, |
| 74 | + keyword |
| 75 | + .arg |
| 76 | + .as_ref() |
| 77 | + .map_or_else(|| keyword.range(), Ranged::range), |
| 78 | + )) |
| 79 | +} |
| 80 | + |
| 81 | +fn removed_argument(checker: &mut Checker, qualname: &QualifiedName, arguments: &Arguments) { |
| 82 | + #[allow(clippy::single_match)] |
| 83 | + match qualname.segments() { |
| 84 | + ["airflow", .., "DAG" | "dag"] => { |
| 85 | + checker.diagnostics.extend(diagnostic_for_argument( |
| 86 | + arguments, |
| 87 | + "schedule_interval", |
| 88 | + Some("schedule"), |
| 89 | + )); |
| 90 | + checker.diagnostics.extend(diagnostic_for_argument( |
| 91 | + arguments, |
| 92 | + "timetable", |
| 93 | + Some("schedule"), |
| 94 | + )); |
| 95 | + } |
| 96 | + _ => {} |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +fn removed_name(checker: &mut Checker, expr: &Expr, ranged: impl Ranged) { |
| 101 | + let result = |
| 102 | + checker |
| 103 | + .semantic() |
| 104 | + .resolve_qualified_name(expr) |
| 105 | + .and_then(|qualname| match qualname.segments() { |
| 106 | + ["airflow", "utils", "dates", "date_range"] => { |
| 107 | + Some((qualname.to_string(), Replacement::None)) |
| 108 | + } |
| 109 | + ["airflow", "utils", "dates", "days_ago"] => Some(( |
| 110 | + qualname.to_string(), |
| 111 | + Replacement::Name("datetime.timedelta()".to_string()), |
| 112 | + )), |
| 113 | + _ => None, |
| 114 | + }); |
| 115 | + if let Some((deprecated, replacement)) = result { |
| 116 | + checker.diagnostics.push(Diagnostic::new( |
| 117 | + Airflow3Removal { |
| 118 | + deprecated, |
| 119 | + replacement, |
| 120 | + }, |
| 121 | + ranged.range(), |
| 122 | + )); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/// AIR302 |
| 127 | +pub(crate) fn removed_in_3(checker: &mut Checker, expr: &Expr) { |
| 128 | + if !checker.semantic().seen_module(Modules::AIRFLOW) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + match expr { |
| 133 | + Expr::Call(ExprCall { |
| 134 | + func, arguments, .. |
| 135 | + }) => { |
| 136 | + if let Some(qualname) = checker.semantic().resolve_qualified_name(func) { |
| 137 | + removed_argument(checker, &qualname, arguments); |
| 138 | + }; |
| 139 | + } |
| 140 | + Expr::Attribute(ExprAttribute { attr: ranged, .. }) => removed_name(checker, expr, ranged), |
| 141 | + ranged @ Expr::Name(_) => removed_name(checker, expr, ranged), |
| 142 | + _ => {} |
| 143 | + } |
| 144 | +} |
0 commit comments