-
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.
feat(AIR303): add rules for names moved to fab provider
* import path (module/package) moved * `airflow.api.auth.backend.basic_auth` → `airflow.providers.fab.auth_manager.api.auth.backend.basic_auth` * `airflow.api.auth.backend.kerberos_auth` → `airflow.providers.fab.auth_manager.api.auth.backend.kerberos_auth` * `airflow.auth.managers.fab.api.auth.backend.kerberos_auth` → `airflow.providers.fab.auth_manager.api.auth.backend.kerberos_auth` * `airflow.auth.managers.fab.security_manager.override` → `airflow.providers.fab.auth_manager.security_manager.override` * other names (e.g., functions, classes) moved * `airflow.www.security.FabAirflowSecurityManagerOverride` → `airflow.providers.fab.auth_manager.security_manager.override.FabAirflowSecurityManagerOverride` * `airflow.auth.managers.fab.fab_auth_manager.FabAuthManager` → `airflow.providers.fab.auth_manager.security_manager.FabAuthManager`
- Loading branch information
Showing
8 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
from airflow.api.auth.backend import basic_auth, kerberos_auth | ||
from airflow.api.auth.backend.basic_auth import auth_current_user | ||
from airflow.auth.managers.fab.api.auth.backend import ( | ||
kerberos_auth as backend_kerberos_auth, | ||
) | ||
from airflow.auth.managers.fab.fab_auth_manager import FabAuthManager | ||
from airflow.auth.managers.fab.security_manager import override as fab_override | ||
from airflow.www.security import FabAirflowSecurityManagerOverride | ||
|
||
basic_auth, kerberos_auth | ||
auth_current_user | ||
backend_kerberos_auth | ||
fab_override | ||
|
||
FabAuthManager | ||
FabAirflowSecurityManagerOverride |
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,7 +1,9 @@ | ||
pub(crate) use dag_schedule_argument::*; | ||
pub(crate) use moved_to_provider_in_3::*; | ||
pub(crate) use removal_in_3::*; | ||
pub(crate) use task_variable_name::*; | ||
|
||
mod dag_schedule_argument; | ||
mod moved_to_provider_in_3; | ||
mod removal_in_3; | ||
mod task_variable_name; |
150 changes: 150 additions & 0 deletions
150
crates/ruff_linter/src/rules/airflow/rules/moved_to_provider_in_3.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,150 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, ViolationMetadata}; | ||
use ruff_python_ast::{Expr, ExprAttribute}; | ||
use ruff_python_semantic::Modules; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[derive(Debug, Eq, PartialEq)] | ||
enum Replacement { | ||
ProviderName(String, String, String), | ||
ImportPathMoved(String, String, String, String), | ||
} | ||
|
||
/// ## What it does | ||
/// Checks for uses of Airflow functions and values that have been moved to it providers. | ||
/// (e.g., apache-airflow-providers-fab) | ||
/// | ||
/// ## Why is this bad? | ||
/// Airflow 3.0 moved various deprecated functions, members, and other | ||
/// values to its providers. The user needs to install the corresponding provider and replace | ||
/// the original usage with the one in the provider | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from airflow.auth.managers.fab.fab_auth_manage import FabAuthManager | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from airflow.providers.fab.auth_manager.fab_auth_manage import FabAuthManager | ||
/// ``` | ||
#[derive(ViolationMetadata)] | ||
pub(crate) struct Airflow3MovedToProvider { | ||
deprecated: String, | ||
replacement: Replacement, | ||
} | ||
|
||
impl Violation for Airflow3MovedToProvider { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Airflow3MovedToProvider { | ||
deprecated, | ||
replacement, | ||
} = self; | ||
match replacement { | ||
Replacement::ProviderName(name, provider_name, provider_version) => { | ||
format!("`{deprecated}` is moved into `{provider_name}` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-{provider_name}=={provider_version}` and use `{name}` instead.") | ||
} | ||
Replacement::ImportPathMoved( | ||
original_path, | ||
new_path, | ||
provider_name, | ||
provider_version, | ||
) => { | ||
format!("Import path `{original_path}` is moved into `{provider_name}` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-{provider_name}=={provider_version}` and import from `{new_path}` instead.") | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn moved_to_provider(checker: &mut Checker, expr: &Expr, ranged: impl Ranged) { | ||
let result = | ||
checker | ||
.semantic() | ||
.resolve_qualified_name(expr) | ||
.and_then(|qualname| match qualname.segments() { | ||
// apache-airflow-providers-fab | ||
["airflow", "www", "security", "FabAirflowSecurityManagerOverride"] => Some(( | ||
qualname.to_string(), | ||
Replacement::ProviderName( | ||
"airflow.providers.fab.auth_manager.security_manager.override.FabAirflowSecurityManagerOverride".to_string(), | ||
"fab".to_string(), | ||
"1.0.0".to_string() | ||
), | ||
)), | ||
["airflow","auth","managers","fab","fab_auth_manager", "FabAuthManager"] => Some(( | ||
qualname.to_string(), | ||
Replacement::ProviderName( | ||
"airflow.providers.fab.auth_manager.security_manager.FabAuthManager".to_string(), | ||
"fab".to_string(), | ||
"1.0.0".to_string() | ||
), | ||
)), | ||
["airflow", "api", "auth", "backend", "basic_auth", ..] => Some(( | ||
qualname.to_string(), | ||
Replacement::ImportPathMoved( | ||
"airflow.api.auth.backend.basic_auth".to_string(), | ||
"airflow.providers.fab.auth_manager.api.auth.backend.basic_auth".to_string(), | ||
"fab".to_string(), | ||
"1.0.0".to_string() | ||
), | ||
)), | ||
["airflow", "api","auth","backend","kerberos_auth", ..] => Some(( | ||
qualname.to_string(), | ||
Replacement::ImportPathMoved( | ||
"airflow.api.auth.backend.kerberos_auth".to_string(), | ||
"airflow.providers.fab.auth_manager.api.auth.backend.kerberos_auth".to_string(), | ||
"fab".to_string(), | ||
"1.0.0".to_string() | ||
), | ||
)), | ||
["airflow", "auth", "managers", "fab", "api", "auth", "backend", "kerberos_auth", ..] => Some(( | ||
qualname.to_string(), | ||
Replacement::ImportPathMoved( | ||
"airflow.auth_manager.api.auth.backend.kerberos_auth".to_string(), | ||
"airflow.providers.fab.auth_manager.api.auth.backend.kerberos_auth".to_string(), | ||
"fab".to_string(), | ||
"1.0.0".to_string() | ||
), | ||
)), | ||
["airflow","auth","managers","fab","security_manager","override", ..] => Some(( | ||
qualname.to_string(), | ||
Replacement::ImportPathMoved( | ||
"airflow.auth.managers.fab.security_manager.override".to_string(), | ||
"airflow.providers.fab.auth_manager.security_manager.override".to_string(), | ||
"fab".to_string(), | ||
"1.0.0".to_string() | ||
), | ||
)), | ||
|
||
_ => None, | ||
}); | ||
if let Some((deprecated, replacement)) = result { | ||
checker.diagnostics.push(Diagnostic::new( | ||
Airflow3MovedToProvider { | ||
deprecated, | ||
replacement, | ||
}, | ||
ranged.range(), | ||
)); | ||
} | ||
} | ||
|
||
/// AIR303 | ||
pub(crate) fn moved_to_provider_in_3(checker: &mut Checker, expr: &Expr) { | ||
if !checker.semantic().seen_module(Modules::AIRFLOW) { | ||
return; | ||
} | ||
|
||
match expr { | ||
Expr::Attribute(ExprAttribute { attr: ranged, .. }) => { | ||
moved_to_provider(checker, expr, ranged); | ||
} | ||
ranged @ Expr::Name(_) => moved_to_provider(checker, expr, ranged), | ||
_ => {} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...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,76 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/airflow/mod.rs | ||
snapshot_kind: text | ||
--- | ||
AIR303.py:10:1: AIR303 Import path `airflow.api.auth.backend.basic_auth` is moved into `fab` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-fab==1.0.0` and import from `airflow.providers.fab.auth_manager.api.auth.backend.basic_auth` instead. | ||
| | ||
8 | from airflow.www.security import FabAirflowSecurityManagerOverride | ||
9 | | ||
10 | basic_auth, kerberos_auth | ||
| ^^^^^^^^^^ AIR303 | ||
11 | auth_current_user | ||
12 | backend_kerberos_auth | ||
| | ||
|
||
AIR303.py:10:13: AIR303 Import path `airflow.api.auth.backend.kerberos_auth` is moved into `fab` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-fab==1.0.0` and import from `airflow.providers.fab.auth_manager.api.auth.backend.kerberos_auth` instead. | ||
| | ||
8 | from airflow.www.security import FabAirflowSecurityManagerOverride | ||
9 | | ||
10 | basic_auth, kerberos_auth | ||
| ^^^^^^^^^^^^^ AIR303 | ||
11 | auth_current_user | ||
12 | backend_kerberos_auth | ||
| | ||
|
||
AIR303.py:11:1: AIR303 Import path `airflow.api.auth.backend.basic_auth` is moved into `fab` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-fab==1.0.0` and import from `airflow.providers.fab.auth_manager.api.auth.backend.basic_auth` instead. | ||
| | ||
10 | basic_auth, kerberos_auth | ||
11 | auth_current_user | ||
| ^^^^^^^^^^^^^^^^^ AIR303 | ||
12 | backend_kerberos_auth | ||
13 | FabAuthManager | ||
| | ||
|
||
AIR303.py:12:1: AIR303 Import path `airflow.auth_manager.api.auth.backend.kerberos_auth` is moved into `fab` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-fab==1.0.0` and import from `airflow.providers.fab.auth_manager.api.auth.backend.kerberos_auth` instead. | ||
| | ||
10 | basic_auth, kerberos_auth | ||
11 | auth_current_user | ||
12 | backend_kerberos_auth | ||
| ^^^^^^^^^^^^^^^^^^^^^ AIR303 | ||
13 | FabAuthManager | ||
14 | fab_override | ||
| | ||
|
||
AIR303.py:13:1: AIR303 `airflow.auth.managers.fab.fab_auth_manager.FabAuthManager` is moved into `fab` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-fab==1.0.0` and use `airflow.providers.fab.auth_manager.security_manager.FabAuthManager` instead. | ||
| | ||
11 | auth_current_user | ||
12 | backend_kerberos_auth | ||
13 | FabAuthManager | ||
| ^^^^^^^^^^^^^^ AIR303 | ||
14 | fab_override | ||
15 | FabAirflowSecurityManagerOverride | ||
| | ||
|
||
AIR303.py:14:1: AIR303 Import path `airflow.auth.managers.fab.security_manager.override` is moved into `fab` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-fab==1.0.0` and import from `airflow.providers.fab.auth_manager.security_manager.override` instead. | ||
| | ||
12 | backend_kerberos_auth | ||
13 | FabAuthManager | ||
14 | fab_override | ||
| ^^^^^^^^^^^^ AIR303 | ||
15 | FabAirflowSecurityManagerOverride | ||
| | ||
|
||
AIR303.py:15:1: AIR303 `airflow.www.security.FabAirflowSecurityManagerOverride` is moved into `fab` provider in Airflow 3.0; | ||
Install `apache-airflow-provider-fab==1.0.0` and use `airflow.providers.fab.auth_manager.security_manager.override.FabAirflowSecurityManagerOverride` instead. | ||
| | ||
13 | FabAuthManager | ||
14 | fab_override | ||
15 | FabAirflowSecurityManagerOverride | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AIR303 | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.