Skip to content

Commit 3445d13

Browse files
authored
[airflow] Add unsafe fix module moved cases (AIR302) (#18093)
<!-- Thank you for contributing to Ruff/ty! 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? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> Add utility functions `generate_import_edit` and `generate_remove_and_runtime_import_edit` to generate the fix needed for the airflow rules. 1. `generate_import_edit` is for the cases where the member name has changed. (e.g., `airflow.datasts.Dataset` to `airflow.sdk.Asset`) It's just extracted from the original logic 2. `generate_remove_and_runtime_import_edit` is for cases where the member name has not changed. (e.g., `airflow.operators.pig_operator.PigOperator` to `airflow.providers.apache.pig.hooks.pig.PigCliHook`) This is newly introduced. As it introduced runtime import, I mark it as an unsafe fix. Under the hook, it tried to find the original import statement, remove it, and add a new import fix --- * rules fix * `airflow.sensors.external_task_sensor.ExternalTaskSensorLink` → `airflow.providers.standard.sensors.external_task.ExternalDagLink` ## Test Plan <!-- How was it tested? --> The existing test fixtures have been updated
1 parent 2c3f091 commit 3445d13

File tree

30 files changed

+3080
-1103
lines changed

30 files changed

+3080
-1103
lines changed

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,30 @@
55
provide_bucket_name,
66
)
77
from airflow.operators.gcs_to_s3 import GCSToS3Operator
8-
from airflow.operators.google_api_to_s3_transfer import (
9-
GoogleApiToS3Operator,
10-
GoogleApiToS3Transfer,
11-
)
12-
from airflow.operators.redshift_to_s3_operator import (
13-
RedshiftToS3Operator,
14-
RedshiftToS3Transfer,
15-
)
8+
from airflow.operators.google_api_to_s3_transfer import GoogleApiToS3Operator
9+
from airflow.operators.redshift_to_s3_operator import RedshiftToS3Operator
1610
from airflow.operators.s3_file_transform_operator import S3FileTransformOperator
17-
from airflow.operators.s3_to_redshift_operator import (
18-
S3ToRedshiftOperator,
19-
S3ToRedshiftTransfer,
20-
)
11+
from airflow.operators.s3_to_redshift_operator import S3ToRedshiftOperator
2112
from airflow.sensors.s3_key_sensor import S3KeySensor
2213

2314
S3Hook()
2415
provide_bucket_name()
2516

2617
GCSToS3Operator()
27-
2818
GoogleApiToS3Operator()
19+
RedshiftToS3Operator()
20+
S3FileTransformOperator()
21+
S3ToRedshiftOperator()
22+
S3KeySensor()
23+
24+
from airflow.operators.google_api_to_s3_transfer import GoogleApiToS3Transfer
25+
2926
GoogleApiToS3Transfer()
3027

31-
RedshiftToS3Operator()
28+
from airflow.operators.redshift_to_s3_operator import RedshiftToS3Transfer
29+
3230
RedshiftToS3Transfer()
3331

34-
S3FileTransformOperator()
32+
from airflow.operators.s3_to_redshift_operator import S3ToRedshiftTransfer
3533

36-
S3ToRedshiftOperator()
3734
S3ToRedshiftTransfer()
38-
39-
S3KeySensor()

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
ConnectorProtocol,
55
DbApiHook,
66
)
7+
8+
ConnectorProtocol()
9+
DbApiHook()
10+
711
from airflow.hooks.dbapi_hook import DbApiHook
812
from airflow.operators.check_operator import SQLCheckOperator
913

10-
ConnectorProtocol()
1114
DbApiHook()
1215
SQLCheckOperator()
1316

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

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,59 @@
1212
)
1313
from airflow.operators.hive_operator import HiveOperator
1414
from airflow.operators.hive_stats_operator import HiveStatsCollectionOperator
15-
from airflow.operators.hive_to_mysql import (
16-
HiveToMySqlOperator,
17-
HiveToMySqlTransfer,
18-
)
15+
from airflow.operators.hive_to_mysql import HiveToMySqlOperator
1916
from airflow.operators.hive_to_samba_operator import HiveToSambaOperator
20-
from airflow.operators.mssql_to_hive import (
21-
MsSqlToHiveOperator,
22-
MsSqlToHiveTransfer,
23-
)
24-
from airflow.operators.mysql_to_hive import (
25-
MySqlToHiveOperator,
26-
MySqlToHiveTransfer,
27-
)
28-
from airflow.operators.s3_to_hive_operator import (
29-
S3ToHiveOperator,
30-
S3ToHiveTransfer,
31-
)
32-
from airflow.sensors.hive_partition_sensor import HivePartitionSensor
33-
from airflow.sensors.metastore_partition_sensor import MetastorePartitionSensor
34-
from airflow.sensors.named_hive_partition_sensor import NamedHivePartitionSensor
35-
36-
closest_ds_partition()
37-
max_partition()
3817

18+
HIVE_QUEUE_PRIORITIES
3919
HiveCliHook()
4020
HiveMetastoreHook()
4121
HiveServer2Hook()
42-
HIVE_QUEUE_PRIORITIES
4322

44-
HiveOperator()
23+
closest_ds_partition()
24+
max_partition()
4525

26+
HiveOperator()
4627
HiveStatsCollectionOperator()
47-
4828
HiveToMySqlOperator()
29+
HiveToSambaOperator()
30+
31+
32+
from airflow.operators.hive_to_mysql import HiveToMySqlTransfer
33+
4934
HiveToMySqlTransfer()
5035

51-
HiveToSambaOperator()
36+
from airflow.operators.mysql_to_hive import MySqlToHiveOperator
37+
38+
MySqlToHiveOperator()
39+
40+
from airflow.operators.mysql_to_hive import MySqlToHiveTransfer
41+
42+
MySqlToHiveTransfer()
43+
44+
from airflow.operators.mssql_to_hive import MsSqlToHiveOperator
5245

5346
MsSqlToHiveOperator()
47+
48+
from airflow.operators.mssql_to_hive import MsSqlToHiveTransfer
49+
5450
MsSqlToHiveTransfer()
5551

56-
MySqlToHiveOperator()
57-
MySqlToHiveTransfer()
52+
from airflow.operators.s3_to_hive_operator import S3ToHiveOperator
5853

5954
S3ToHiveOperator()
55+
56+
from airflow.operators.s3_to_hive_operator import S3ToHiveTransfer
57+
6058
S3ToHiveTransfer()
6159

60+
from airflow.sensors.hive_partition_sensor import HivePartitionSensor
61+
6262
HivePartitionSensor()
6363

64+
from airflow.sensors.metastore_partition_sensor import MetastorePartitionSensor
65+
6466
MetastorePartitionSensor()
6567

68+
from airflow.sensors.named_hive_partition_sensor import NamedHivePartitionSensor
69+
6670
NamedHivePartitionSensor()

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@
1616
from airflow.kubernetes.kubernetes_helper_functions import (
1717
add_pod_suffix,
1818
annotations_for_logging_task_metadata,
19-
annotations_to_key,
2019
create_pod_id,
21-
get_logs_task_metadata,
22-
rand_str,
23-
)
24-
from airflow.kubernetes.pod import (
25-
Port,
26-
Resources,
2720
)
2821

2922
ALL_NAMESPACES
@@ -37,21 +30,13 @@
3730
get_kube_client()
3831

3932
add_pod_suffix()
40-
create_pod_id()
41-
4233
annotations_for_logging_task_metadata()
43-
annotations_to_key()
44-
get_logs_task_metadata()
45-
rand_str()
46-
47-
Port()
48-
Resources()
34+
create_pod_id()
4935

5036

5137
from airflow.kubernetes.pod_generator import (
5238
PodDefaults,
5339
PodGenerator,
54-
PodGeneratorDeprecated,
5540
add_pod_suffix,
5641
datetime_to_label_safe_datestring,
5742
extend_object_field,
@@ -61,18 +46,16 @@
6146
rand_str,
6247
)
6348

49+
PodDefaults()
50+
PodGenerator()
51+
add_pod_suffix()
6452
datetime_to_label_safe_datestring()
6553
extend_object_field()
6654
label_safe_datestring_to_datetime()
6755
make_safe_label_value()
6856
merge_objects()
69-
PodGenerator()
70-
PodDefaults()
71-
PodGeneratorDeprecated()
72-
add_pod_suffix()
7357
rand_str()
7458

75-
7659
from airflow.kubernetes.pod_generator_deprecated import (
7760
PodDefaults,
7861
PodGenerator,
@@ -90,7 +73,6 @@
9073
PodLauncher()
9174
PodStatus()
9275

93-
9476
from airflow.kubernetes.pod_launcher_deprecated import (
9577
PodDefaults,
9678
PodLauncher,
@@ -115,3 +97,17 @@
11597
Secret()
11698
Volume()
11799
VolumeMount()
100+
101+
from airflow.kubernetes.kubernetes_helper_functions import (
102+
annotations_to_key,
103+
get_logs_task_metadata,
104+
rand_str,
105+
)
106+
107+
annotations_to_key()
108+
get_logs_task_metadata()
109+
rand_str()
110+
111+
from airflow.kubernetes.pod_generator import PodGeneratorDeprecated
112+
113+
PodGeneratorDeprecated()

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
TriggerDagRunLink,
66
TriggerDagRunOperator,
77
)
8-
from airflow.operators.dummy import (
9-
DummyOperator,
10-
EmptyOperator,
11-
)
128
from airflow.operators.latest_only_operator import LatestOnlyOperator
139
from airflow.operators.python_operator import (
1410
BranchPythonOperator,
@@ -19,15 +15,12 @@
1915
from airflow.sensors.external_task_sensor import (
2016
ExternalTaskMarker,
2117
ExternalTaskSensor,
22-
ExternalTaskSensorLink,
2318
)
2419

2520
BashOperator()
2621

2722
TriggerDagRunLink()
2823
TriggerDagRunOperator()
29-
DummyOperator()
30-
EmptyOperator()
3124

3225
LatestOnlyOperator()
3326

@@ -38,25 +31,48 @@
3831

3932
ExternalTaskMarker()
4033
ExternalTaskSensor()
41-
ExternalTaskSensorLink()
4234

43-
from airflow.operators.dummy_operator import (
44-
DummyOperator,
45-
EmptyOperator,
46-
)
47-
48-
DummyOperator()
49-
EmptyOperator()
5035

5136
from airflow.hooks.subprocess import SubprocessResult
37+
5238
SubprocessResult()
39+
5340
from airflow.hooks.subprocess import working_directory
41+
5442
working_directory()
43+
5544
from airflow.operators.datetime import target_times_as_dates
45+
5646
target_times_as_dates()
47+
5748
from airflow.operators.trigger_dagrun import TriggerDagRunLink
49+
5850
TriggerDagRunLink()
51+
5952
from airflow.sensors.external_task import ExternalTaskSensorLink
53+
6054
ExternalTaskSensorLink()
55+
6156
from airflow.sensors.time_delta import WaitSensor
62-
WaitSensor()
57+
58+
WaitSensor()
59+
60+
from airflow.operators.dummy import DummyOperator
61+
62+
DummyOperator()
63+
64+
from airflow.operators.dummy import EmptyOperator
65+
66+
EmptyOperator()
67+
68+
from airflow.operators.dummy_operator import DummyOperator
69+
70+
DummyOperator()
71+
72+
from airflow.operators.dummy_operator import EmptyOperator
73+
74+
EmptyOperator()
75+
76+
from airflow.sensors.external_task_sensor import ExternalTaskSensorLink
77+
78+
ExternalTaskSensorLink()

0 commit comments

Comments
 (0)