This is an automated email from the ASF dual-hosted git repository.

eladkal pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new a9bbb430fc Replace try - except pass by contextlib.suppress in 
providers (#33980)
a9bbb430fc is described below

commit a9bbb430fcf6df7ac2677edfe5b0402c23cfe8e2
Author: Hussein Awala <[email protected]>
AuthorDate: Fri Sep 1 08:08:20 2023 +0200

    Replace try - except pass by contextlib.suppress in providers (#33980)
---
 airflow/providers/amazon/aws/hooks/s3.py                    | 4 +---
 airflow/providers/celery/executors/celery_executor_utils.py | 9 +++------
 airflow/providers/google/cloud/hooks/mlengine.py            | 6 +++---
 airflow/providers/google/cloud/hooks/stackdriver.py         | 5 ++---
 4 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/airflow/providers/amazon/aws/hooks/s3.py 
b/airflow/providers/amazon/aws/hooks/s3.py
index e2a46f54eb..5a19fc8c85 100644
--- a/airflow/providers/amazon/aws/hooks/s3.py
+++ b/airflow/providers/amazon/aws/hooks/s3.py
@@ -41,10 +41,8 @@ from urllib.parse import urlsplit
 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
diff --git a/airflow/providers/celery/executors/celery_executor_utils.py 
b/airflow/providers/celery/executors/celery_executor_utils.py
index df261323dc..a3b01a3bb5 100644
--- a/airflow/providers/celery/executors/celery_executor_utils.py
+++ b/airflow/providers/celery/executors/celery_executor_utils.py
@@ -22,6 +22,7 @@ Much of this code is expensive to import/load, be careful 
where this module is i
 
 from __future__ import annotations
 
+import contextlib
 import logging
 import math
 import os
@@ -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
diff --git a/airflow/providers/google/cloud/hooks/mlengine.py 
b/airflow/providers/google/cloud/hooks/mlengine.py
index fdc339299d..5b3fc480ed 100644
--- a/airflow/providers/google/cloud/hooks/mlengine.py
+++ b/airflow/providers/google/cloud/hooks/mlengine.py
@@ -18,6 +18,7 @@
 """This module contains a Google ML Engine Hook."""
 from __future__ import annotations
 
+import contextlib
 import logging
 import random
 import time
@@ -561,10 +562,9 @@ class MLEngineAsyncHook(GoogleBaseAsyncHook):
             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
 
diff --git a/airflow/providers/google/cloud/hooks/stackdriver.py 
b/airflow/providers/google/cloud/hooks/stackdriver.py
index b16a2233ee..e854e48112 100644
--- a/airflow/providers/google/cloud/hooks/stackdriver.py
+++ b/airflow/providers/google/cloud/hooks/stackdriver.py
@@ -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
 
@@ -295,15 +296,13 @@ class StackdriverHook(GoogleBaseHook):
                     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:

Reply via email to