Skip to content

Commit

Permalink
Replace try - except pass by contextlib.suppress in providers (#33980)
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-awala committed Sep 1, 2023
1 parent 687977f commit a9bbb43
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
4 changes: 1 addition & 3 deletions airflow/providers/amazon/aws/hooks/s3.py
Expand Up @@ -41,10 +41,8 @@
from uuid import uuid4

if TYPE_CHECKING:
try:
with suppress(ImportError):
from aiobotocore.client import AioBaseClient
except ImportError:
pass

from asgiref.sync import sync_to_async
from boto3.s3.transfer import TransferConfig
Expand Down
9 changes: 3 additions & 6 deletions airflow/providers/celery/executors/celery_executor_utils.py
Expand Up @@ -22,6 +22,7 @@

from __future__ import annotations

import contextlib
import logging
import math
import os
Expand Down Expand Up @@ -99,15 +100,11 @@ def on_celery_import_modules(*args, **kwargs):
import airflow.operators.python
import airflow.operators.subdag # noqa: F401

try:
with contextlib.suppress(ImportError):
import numpy # noqa: F401
except ImportError:
pass

try:
with contextlib.suppress(ImportError):
import kubernetes.client # noqa: F401
except ImportError:
pass


@app.task
Expand Down
6 changes: 3 additions & 3 deletions airflow/providers/google/cloud/hooks/mlengine.py
Expand Up @@ -18,6 +18,7 @@
"""This module contains a Google ML Engine Hook."""
from __future__ import annotations

import contextlib
import logging
import random
import time
Expand Down Expand Up @@ -561,10 +562,9 @@ async def _get_link(self, url: str, session: Session):
headers = {
"Authorization": f"Bearer {await token.get()}",
}
try:
with contextlib.suppress(AirflowException):
# suppress AirflowException because we don't want to raise exception
job = await session_aio.get(url=url, headers=headers)
except AirflowException:
pass # Because the job may not be visible in system yet

return job

Expand Down
5 changes: 2 additions & 3 deletions airflow/providers/google/cloud/hooks/stackdriver.py
Expand Up @@ -18,6 +18,7 @@
"""This module contains Google Cloud Stackdriver operators."""
from __future__ import annotations

import contextlib
import json
from typing import TYPE_CHECKING, Any, Sequence

Expand Down Expand Up @@ -295,15 +296,13 @@ def upsert_alert(
policy.notification_channels[i] = new_channel

if policy.name in existing_policies:
try:
with contextlib.suppress(InvalidArgument):
policy_client.update_alert_policy(
request={"alert_policy": policy},
retry=retry,
timeout=timeout,
metadata=metadata,
)
except InvalidArgument:
pass
else:
policy.name = None
for condition in policy.conditions:
Expand Down

0 comments on commit a9bbb43

Please sign in to comment.