Skip to content

Commit a9bbb43

Browse files
Replace try - except pass by contextlib.suppress in providers (#33980)
1 parent 687977f commit a9bbb43

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

β€Žairflow/providers/amazon/aws/hooks/s3.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141
from uuid import uuid4
4242

4343
if TYPE_CHECKING:
44-
try:
44+
with suppress(ImportError):
4545
from aiobotocore.client import AioBaseClient
46-
except ImportError:
47-
pass
4846

4947
from asgiref.sync import sync_to_async
5048
from boto3.s3.transfer import TransferConfig

β€Žairflow/providers/celery/executors/celery_executor_utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from __future__ import annotations
2424

25+
import contextlib
2526
import logging
2627
import math
2728
import os
@@ -99,15 +100,11 @@ def on_celery_import_modules(*args, **kwargs):
99100
import airflow.operators.python
100101
import airflow.operators.subdag # noqa: F401
101102

102-
try:
103+
with contextlib.suppress(ImportError):
103104
import numpy # noqa: F401
104-
except ImportError:
105-
pass
106105

107-
try:
106+
with contextlib.suppress(ImportError):
108107
import kubernetes.client # noqa: F401
109-
except ImportError:
110-
pass
111108

112109

113110
@app.task

β€Žairflow/providers/google/cloud/hooks/mlengine.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""This module contains a Google ML Engine Hook."""
1919
from __future__ import annotations
2020

21+
import contextlib
2122
import logging
2223
import random
2324
import time
@@ -561,10 +562,9 @@ async def _get_link(self, url: str, session: Session):
561562
headers = {
562563
"Authorization": f"Bearer {await token.get()}",
563564
}
564-
try:
565+
with contextlib.suppress(AirflowException):
566+
# suppress AirflowException because we don't want to raise exception
565567
job = await session_aio.get(url=url, headers=headers)
566-
except AirflowException:
567-
pass # Because the job may not be visible in system yet
568568

569569
return job
570570

β€Žairflow/providers/google/cloud/hooks/stackdriver.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""This module contains Google Cloud Stackdriver operators."""
1919
from __future__ import annotations
2020

21+
import contextlib
2122
import json
2223
from typing import TYPE_CHECKING, Any, Sequence
2324

@@ -295,15 +296,13 @@ def upsert_alert(
295296
policy.notification_channels[i] = new_channel
296297

297298
if policy.name in existing_policies:
298-
try:
299+
with contextlib.suppress(InvalidArgument):
299300
policy_client.update_alert_policy(
300301
request={"alert_policy": policy},
301302
retry=retry,
302303
timeout=timeout,
303304
metadata=metadata,
304305
)
305-
except InvalidArgument:
306-
pass
307306
else:
308307
policy.name = None
309308
for condition in policy.conditions:

0 commit comments

Comments
 (0)