Skip to content

Commit e8c79ed

Browse files
authored
[airflow] Get rid of Replacement::Name and replace them with Replacement::AutoImport for enabling auto fixing (AIR301, AIR311) (#17941)
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> Similiar to astral-sh/ruff#17941. `Replacement::Name` was designed for linting only. Now, we also want to fix the user code. It would be easier to replace it with a better AutoImport struct whenever possible. On the other hand, `AIR301` and `AIR311` contain attribute changes that can still use a struct like `Replacement::Name`. To reduce the confusion, I also updated it as `Replacement::AttrName` Some of the original `Replacement::Name` has been replaced as `Replacement::Message` as they're not directly mapping and the message has now been moved to `help` ## Test Plan <!-- How was it tested? --> The test fixtures have been updated
1 parent ad61506 commit e8c79ed

7 files changed

Lines changed: 428 additions & 345 deletions

crates/ruff_linter/resources/test/fixtures/airflow/AIR301_names.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
from airflow.datasets import DatasetAliasEvent
2525
from airflow.hooks.base_hook import BaseHook
2626
from airflow.operators.subdag import SubDagOperator
27-
from airflow.providers.mysql.datasets import mysql
28-
from airflow.providers.postgres.datasets import postgres
29-
from airflow.providers.trino.datasets import trino
3027
from airflow.secrets.local_filesystem import LocalFilesystemBackend
3128
from airflow.sensors.base_sensor_operator import BaseSensorOperator
3229
from airflow.triggers.external_task import TaskStateTrigger
@@ -78,14 +75,6 @@
7875
# airflow.operators.subdag.*
7976
SubDagOperator()
8077

81-
# airflow.providers.mysql
82-
mysql.sanitize_uri
83-
84-
# airflow.providers.postgres
85-
postgres.sanitize_uri
86-
87-
# airflow.providers.trino
88-
trino.sanitize_uri
8978

9079
# airflow.secrets
9180
# get_connection
@@ -155,3 +144,18 @@
155144
from airflow.operators.python import get_current_context
156145

157146
get_current_context()
147+
148+
# airflow.providers.mysql
149+
from airflow.providers.mysql.datasets.mysql import sanitize_uri
150+
151+
sanitize_uri
152+
153+
# airflow.providers.postgres
154+
from airflow.providers.postgres.datasets.postgres import sanitize_uri
155+
156+
sanitize_uri
157+
158+
# airflow.providers.trino
159+
from airflow.providers.trino.datasets.trino import sanitize_uri
160+
161+
sanitize_uri

crates/ruff_linter/src/rules/airflow/helpers.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ use ruff_python_semantic::SemanticModel;
88

99
#[derive(Clone, Debug, Eq, PartialEq)]
1010
pub(crate) enum Replacement {
11+
// There's no replacement or suggestion other than removal
1112
None,
12-
Name(&'static str),
13+
// The attribute name of a class has been changed.
14+
AttrName(&'static str),
15+
// Additional information. Used when there's replacement but they're not direct mapping.
1316
Message(&'static str),
17+
// Symbols updated in Airflow 3 with replacement
18+
// e.g., `airflow.datasets.Dataset` to `airflow.sdk.Asset`
1419
AutoImport {
1520
module: &'static str,
1621
name: &'static str,
1722
},
23+
// Symbols updated in Airflow 3 with only module changed. Used when we want to match multiple names.
24+
// e.g., `airflow.configuration.as_dict | get` to `airflow.configuration.conf.as_dict | get`
1825
SourceModuleMoved {
1926
module: &'static str,
2027
name: String,

0 commit comments

Comments
 (0)