From fa5e54c4c57631de353102af56633f05346685f9 Mon Sep 17 00:00:00 2001 From: Hussein Awala Date: Sun, 3 Sep 2023 22:09:07 +0200 Subject: [PATCH] Combine similar if logics in providers (#33987) --- airflow/providers/amazon/aws/utils/sqs.py | 4 +--- airflow/providers/celery/executors/celery_executor.py | 4 +--- airflow/providers/cncf/kubernetes/operators/pod.py | 8 ++------ airflow/providers/google/cloud/log/gcs_task_handler.py | 6 +++--- .../providers/google/cloud/transfers/cassandra_to_gcs.py | 4 +--- airflow/providers/oracle/hooks/oracle.py | 4 +--- 6 files changed, 9 insertions(+), 21 deletions(-) diff --git a/airflow/providers/amazon/aws/utils/sqs.py b/airflow/providers/amazon/aws/utils/sqs.py index 0ae5e7ac98d7b..078baa27482d7 100644 --- a/airflow/providers/amazon/aws/utils/sqs.py +++ b/airflow/providers/amazon/aws/utils/sqs.py @@ -38,9 +38,7 @@ def process_response( :param response: The response from SQS :return: The processed response """ - if not isinstance(response, dict): - return [] - elif "Messages" not in response: + if not isinstance(response, dict) or "Messages" not in response: return [] messages = response["Messages"] diff --git a/airflow/providers/celery/executors/celery_executor.py b/airflow/providers/celery/executors/celery_executor.py index a73e7dbb7368e..a2caf92c757e7 100644 --- a/airflow/providers/celery/executors/celery_executor.py +++ b/airflow/providers/celery/executors/celery_executor.py @@ -368,9 +368,7 @@ def update_task_state(self, key: TaskInstanceKey, state: str, info: Any) -> None self.success(key, info) elif state in (celery_states.FAILURE, celery_states.REVOKED): self.fail(key, info) - elif state == celery_states.STARTED: - pass - elif state == celery_states.PENDING: + elif state in (celery_states.STARTEDstate, celery_states.PENDING): pass else: self.log.info("Unexpected state for %s: %s", key, state) diff --git a/airflow/providers/cncf/kubernetes/operators/pod.py b/airflow/providers/cncf/kubernetes/operators/pod.py index 122315fe407f1..3f3438b1b7eea 100644 --- a/airflow/providers/cncf/kubernetes/operators/pod.py +++ b/airflow/providers/cncf/kubernetes/operators/pod.py @@ -1003,14 +1003,10 @@ def __enter__(self): def __exit__(self, exctype, excinst, exctb): error = exctype is not None matching_error = error and issubclass(exctype, self._exceptions) - if error and not matching_error: - return False - elif matching_error and self.reraise: + if (error and not matching_error) or (matching_error and self.reraise): return False elif matching_error: self.exception = excinst logger = logging.getLogger(__name__) logger.exception(excinst) - return True - else: - return True + return True diff --git a/airflow/providers/google/cloud/log/gcs_task_handler.py b/airflow/providers/google/cloud/log/gcs_task_handler.py index e6c8b1833f2a0..cebe9a829ea5d 100644 --- a/airflow/providers/google/cloud/log/gcs_task_handler.py +++ b/airflow/providers/google/cloud/log/gcs_task_handler.py @@ -263,8 +263,8 @@ def no_log_found(exc): :meta private: """ - if exc.args and isinstance(exc.args[0], str) and "No such object" in exc.args[0]: - return True - elif getattr(exc, "resp", {}).get("status") == "404": + if (exc.args and isinstance(exc.args[0], str) and "No such object" in exc.args[0]) or getattr( + exc, "resp", {} + ).get("status") == "404": return True return False diff --git a/airflow/providers/google/cloud/transfers/cassandra_to_gcs.py b/airflow/providers/google/cloud/transfers/cassandra_to_gcs.py index 620edfbe1558f..8c53b929f47fa 100644 --- a/airflow/providers/google/cloud/transfers/cassandra_to_gcs.py +++ b/airflow/providers/google/cloud/transfers/cassandra_to_gcs.py @@ -258,9 +258,7 @@ def generate_data_dict(self, names: Iterable[str], values: Any) -> dict[str, Any def convert_value(self, value: Any | None) -> Any | None: """Convert value to BQ type.""" - if not value: - return value - elif isinstance(value, (str, int, float, bool, dict)): + if not value or isinstance(value, (str, int, float, bool, dict)): return value elif isinstance(value, bytes): return b64encode(value).decode("ascii") diff --git a/airflow/providers/oracle/hooks/oracle.py b/airflow/providers/oracle/hooks/oracle.py index d91bccecc00d4..2b6bfdb00904c 100644 --- a/airflow/providers/oracle/hooks/oracle.py +++ b/airflow/providers/oracle/hooks/oracle.py @@ -295,9 +295,7 @@ def insert_rows( for cell in row: if isinstance(cell, str): lst.append("'" + str(cell).replace("'", "''") + "'") - elif cell is None: - lst.append("NULL") - elif isinstance(cell, float) and math.isnan(cell): # coerce numpy NaN to NULL + elif cell is None or isinstance(cell, float) and math.isnan(cell): # coerce numpy NaN to NULL lst.append("NULL") elif numpy and isinstance(cell, numpy.datetime64): lst.append("'" + str(cell) + "'")