You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by hu...@apache.org on 2023/09/01 06:45:47 UTC
[airflow] branch main updated: Replace try - except pass by contextlib.suppress in tests (#33981)
This is an automated email from the ASF dual-hosted git repository.
husseinawala 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 6be503eaa7 Replace try - except pass by contextlib.suppress in tests (#33981)
6be503eaa7 is described below
commit 6be503eaa71490d4ebb3178cd18dcd632daeed77
Author: Hussein Awala <hu...@awala.fr>
AuthorDate: Fri Sep 1 08:45:40 2023 +0200
Replace try - except pass by contextlib.suppress in tests (#33981)
---
tests/cli/commands/test_task_command.py | 13 +++------
tests/dag_processing/test_job_runner.py | 9 ++----
tests/jobs/test_scheduler_job.py | 5 ++--
tests/listeners/test_listeners.py | 6 ++--
tests/models/test_taskinstance.py | 33 ++++++----------------
.../amazon/aws/deferrable/hooks/test_base_aws.py | 5 ++--
tests/providers/http/hooks/test_http.py | 9 ++----
tests/providers/sftp/operators/test_sftp.py | 17 ++++-------
.../providers/amazon/aws/example_quicksight.py | 7 ++---
.../example_taskflow_api_docker_virtualenv.py | 6 ++--
tests/test_utils/system_tests_class.py | 5 ++--
11 files changed, 39 insertions(+), 76 deletions(-)
diff --git a/tests/cli/commands/test_task_command.py b/tests/cli/commands/test_task_command.py
index c02ccec203..f8c38003b0 100644
--- a/tests/cli/commands/test_task_command.py
+++ b/tests/cli/commands/test_task_command.py
@@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations
+import contextlib
import io
import json
import logging
@@ -660,10 +661,8 @@ class TestLogsfromTaskRunCommand:
self.root_filters = root.filters.copy()
self.root_level = root.level
- try:
+ with contextlib.suppress(OSError):
os.remove(self.ti_log_file_path)
- except OSError:
- pass
def teardown_method(self) -> None:
root = self.root_logger
@@ -672,10 +671,8 @@ class TestLogsfromTaskRunCommand:
root.filters[:] = self.root_filters
reset(self.dag_id)
- try:
+ with contextlib.suppress(OSError):
os.remove(self.ti_log_file_path)
- except OSError:
- pass
def assert_log_line(self, text, logs_list, expect_from_logging_mixin=False):
"""
@@ -864,10 +861,8 @@ class TestLogsfromTaskRunCommand:
assert os.path.exists(log_file_path)
finally:
- try:
+ with contextlib.suppress(OSError):
os.remove(log_file_path)
- except OSError:
- pass
@mock.patch.object(task_command, "_run_task_by_selected_method")
def test_root_logger_restored(self, run_task_mock, caplog):
diff --git a/tests/dag_processing/test_job_runner.py b/tests/dag_processing/test_job_runner.py
index 481b740ca6..a30c4a4b9f 100644
--- a/tests/dag_processing/test_job_runner.py
+++ b/tests/dag_processing/test_job_runner.py
@@ -18,6 +18,7 @@
from __future__ import annotations
import collections
+import contextlib
import logging
import multiprocessing
import os
@@ -1343,10 +1344,8 @@ class TestDagFileProcessorAgent:
async_mode = "sqlite" not in conf.get("database", "sql_alchemy_conn")
log_file_loc = conf.get("logging", "DAG_PROCESSOR_MANAGER_LOG_LOCATION")
- try:
+ with contextlib.suppress(OSError):
os.remove(log_file_loc)
- except OSError:
- pass
# Starting dag processing with 0 max_runs to avoid redundant operations.
processor_agent = DagFileProcessorAgent(
@@ -1393,10 +1392,8 @@ class TestDagFileProcessorAgent:
async_mode = "sqlite" not in conf.get("database", "sql_alchemy_conn")
log_file_loc = conf.get("logging", "DAG_PROCESSOR_MANAGER_LOG_LOCATION")
- try:
+ with contextlib.suppress(OSError):
os.remove(log_file_loc)
- except OSError:
- pass
# Starting dag processing with 0 max_runs to avoid redundant operations.
processor_agent = DagFileProcessorAgent(test_dag_path, 0, timedelta(days=365), [], False, async_mode)
diff --git a/tests/jobs/test_scheduler_job.py b/tests/jobs/test_scheduler_job.py
index 3ef80030e0..94cf3b6728 100644
--- a/tests/jobs/test_scheduler_job.py
+++ b/tests/jobs/test_scheduler_job.py
@@ -18,6 +18,7 @@
from __future__ import annotations
import collections
+import contextlib
import datetime
import logging
import os
@@ -3016,10 +3017,8 @@ class TestSchedulerJob:
ti.task = dag_task1
def run_with_error(ti, ignore_ti_state=False):
- try:
+ with contextlib.suppress(AirflowException):
ti.run(ignore_ti_state=ignore_ti_state)
- except AirflowException:
- pass
assert ti.try_number == 1
# At this point, scheduler has tried to schedule the task once and
diff --git a/tests/listeners/test_listeners.py b/tests/listeners/test_listeners.py
index f2958f2c01..ac147d08f1 100644
--- a/tests/listeners/test_listeners.py
+++ b/tests/listeners/test_listeners.py
@@ -16,6 +16,7 @@
# under the License.
from __future__ import annotations
+import contextlib
import os
import pytest as pytest
@@ -87,10 +88,9 @@ def test_multiple_listeners(create_task_instance, session=None):
job = Job()
job_runner = MockJobRunner(job=job)
- try:
+ with contextlib.suppress(NotImplementedError):
+ # suppress NotImplementedError: just for lifecycle
run_job(job=job, execute_callable=job_runner._execute)
- except NotImplementedError:
- pass # just for lifecycle
assert full_listener.started_component is job
assert lifecycle_listener.started_component is job
diff --git a/tests/models/test_taskinstance.py b/tests/models/test_taskinstance.py
index e50917e101..d9626ee260 100644
--- a/tests/models/test_taskinstance.py
+++ b/tests/models/test_taskinstance.py
@@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations
+import contextlib
import datetime
import operator
import os
@@ -593,10 +594,8 @@ class TestTaskInstance:
)
def run_with_error(ti):
- try:
+ with contextlib.suppress(AirflowException):
ti.run()
- except AirflowException:
- pass
ti = dag_maker.create_dagrun(execution_date=timezone.utcnow()).task_instances[0]
ti.task = task
@@ -632,10 +631,8 @@ class TestTaskInstance:
)
def run_with_error(ti):
- try:
+ with contextlib.suppress(AirflowException):
ti.run()
- except AirflowException:
- pass
ti = dag_maker.create_dagrun(execution_date=timezone.utcnow()).task_instances[0]
ti.task = task
@@ -1873,10 +1870,8 @@ class TestTaskInstance:
ti = dag_maker.create_dagrun(execution_date=timezone.utcnow()).task_instances[0]
ti.task = task
- try:
+ with contextlib.suppress(AirflowException):
ti.run()
- except AirflowException:
- pass
(email, title, body), _ = mock_send_email.call_args
assert email == "to"
@@ -1903,10 +1898,8 @@ class TestTaskInstance:
opener = mock_open(read_data="template: {{ti.task_id}}")
with patch("airflow.models.taskinstance.open", opener, create=True):
- try:
+ with contextlib.suppress(AirflowException):
ti.run()
- except AirflowException:
- pass
(email, title, body), _ = mock_send_email.call_args
assert email == "to"
@@ -1928,18 +1921,14 @@ class TestTaskInstance:
opener = mock_open(read_data="template: {{ti.task_id}}")
opener.side_effect = FileNotFoundError
with patch("airflow.models.taskinstance.open", opener, create=True):
- try:
+ with contextlib.suppress(AirflowException):
ti.run()
- except AirflowException:
- pass
(email_error, title_error, body_error), _ = mock_send_email.call_args
# Rerun task without any error and no template file
- try:
+ with contextlib.suppress(AirflowException):
ti.run()
- except AirflowException:
- pass
(email_default, title_default, body_default), _ = mock_send_email.call_args
@@ -2819,10 +2808,8 @@ class TestTaskInstance:
)
ti = dag_maker.create_dagrun(execution_date=timezone.utcnow()).task_instances[0]
ti.task = task
- try:
+ with contextlib.suppress(AirflowException):
ti.run()
- except AirflowFailException:
- pass # expected
assert State.FAILED == ti.state
def test_retries_on_other_exceptions(self, dag_maker):
@@ -2837,10 +2824,8 @@ class TestTaskInstance:
)
ti = dag_maker.create_dagrun(execution_date=timezone.utcnow()).task_instances[0]
ti.task = task
- try:
+ with contextlib.suppress(AirflowException):
ti.run()
- except AirflowException:
- pass # expected
assert State.UP_FOR_RETRY == ti.state
def test_stacktrace_on_failure_starts_with_task_execute_method(self, dag_maker):
diff --git a/tests/providers/amazon/aws/deferrable/hooks/test_base_aws.py b/tests/providers/amazon/aws/deferrable/hooks/test_base_aws.py
index 340564de8b..f9daffa26f 100644
--- a/tests/providers/amazon/aws/deferrable/hooks/test_base_aws.py
+++ b/tests/providers/amazon/aws/deferrable/hooks/test_base_aws.py
@@ -16,6 +16,7 @@
# under the License.
from __future__ import annotations
+import contextlib
from unittest import mock
from unittest.mock import ANY
@@ -26,10 +27,8 @@ from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseAsyncHook
pytest.importorskip("aiobotocore")
-try:
+with contextlib.suppress(ImportError):
from aiobotocore.credentials import AioCredentials
-except ImportError:
- pass
class TestAwsBaseAsyncHook:
diff --git a/tests/providers/http/hooks/test_http.py b/tests/providers/http/hooks/test_http.py
index e1606aefa9..345afa54de 100644
--- a/tests/providers/http/hooks/test_http.py
+++ b/tests/providers/http/hooks/test_http.py
@@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations
+import contextlib
import functools
import json
import logging
@@ -90,10 +91,8 @@ class TestHttpHook:
):
expected_url = "http://test.com:1234/some/endpoint"
for endpoint in ["some/endpoint", "/some/endpoint"]:
- try:
+ with contextlib.suppress(MissingSchema):
self.get_hook.run(endpoint)
- except MissingSchema:
- pass
mock_request.assert_called_once_with(
mock.ANY, expected_url, headers=mock.ANY, params=mock.ANY
@@ -128,10 +127,8 @@ class TestHttpHook:
"airflow.hooks.base.BaseHook.get_connection", side_effect=get_airflow_connection_with_port
):
data = "test params"
- try:
+ with contextlib.suppress(MissingSchema, InvalidURL):
self.get_lowercase_hook.run("v1/test", data=data)
- except (MissingSchema, InvalidURL):
- pass
mock_requests.assert_called_once_with(mock.ANY, mock.ANY, headers=mock.ANY, params=data)
def test_hook_uses_provided_header(self):
diff --git a/tests/providers/sftp/operators/test_sftp.py b/tests/providers/sftp/operators/test_sftp.py
index 8adb93f7db..74f23a77fb 100644
--- a/tests/providers/sftp/operators/test_sftp.py
+++ b/tests/providers/sftp/operators/test_sftp.py
@@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations
+import contextlib
import os
import socket
from base64 import b64encode
@@ -327,10 +328,8 @@ class TestSFTPOperator:
operation=SFTPOperation.PUT,
dag=dag,
)
- try:
+ with contextlib.suppress(Exception):
task_1.execute(None)
- except Exception:
- pass
assert task_1.sftp_hook.ssh_conn_id == TEST_CONN_ID
task_2 = SFTPOperator(
@@ -341,10 +340,8 @@ class TestSFTPOperator:
operation=SFTPOperation.PUT,
dag=dag,
)
- try:
+ with contextlib.suppress(Exception):
task_2.execute(None)
- except Exception:
- pass
assert task_2.sftp_hook.ssh_conn_id == TEST_CONN_ID
# if both valid ssh_hook and ssh_conn_id are provided, ignore ssh_conn_id
@@ -357,10 +354,8 @@ class TestSFTPOperator:
operation=SFTPOperation.PUT,
dag=dag,
)
- try:
+ with contextlib.suppress(Exception):
task_3.execute(None)
- except Exception:
- pass
assert task_3.sftp_hook.ssh_conn_id == self.hook.ssh_conn_id
# Exception should be raised if operation is invalid
@@ -399,10 +394,8 @@ class TestSFTPOperator:
operation=SFTPOperation.PUT,
dag=dag,
)
- try:
+ with contextlib.suppress(Exception):
task_6.execute(None)
- except Exception:
- pass
assert task_6.sftp_hook.remote_host == "remotehost"
def test_unequal_local_remote_file_paths(self):
diff --git a/tests/system/providers/amazon/aws/example_quicksight.py b/tests/system/providers/amazon/aws/example_quicksight.py
index bc3cbd2abd..9a3f92c598 100644
--- a/tests/system/providers/amazon/aws/example_quicksight.py
+++ b/tests/system/providers/amazon/aws/example_quicksight.py
@@ -16,6 +16,7 @@
# under the License.
from __future__ import annotations
+import contextlib
import json
from datetime import datetime
@@ -114,15 +115,13 @@ def delete_dataset(aws_account_id: str, dataset_name: str):
@task(trigger_rule=TriggerRule.ALL_DONE)
def delete_ingestion(aws_account_id: str, dataset_name: str, ingestion_name: str) -> None:
client = boto3.client("quicksight")
- try:
+ with contextlib.suppress(client.exceptions.ResourceNotFoundException):
+ # suppress ResourceNotFoundException: Ingestion has already terminated on its own.
client.cancel_ingestion(
AwsAccountId=aws_account_id,
DataSetId=dataset_name,
IngestionId=ingestion_name,
)
- except client.exceptions.ResourceNotFoundException:
- # Ingestion has already terminated on its own.
- pass
with DAG(
diff --git a/tests/system/providers/docker/example_taskflow_api_docker_virtualenv.py b/tests/system/providers/docker/example_taskflow_api_docker_virtualenv.py
index 5e918c80a7..bee2d05581 100644
--- a/tests/system/providers/docker/example_taskflow_api_docker_virtualenv.py
+++ b/tests/system/providers/docker/example_taskflow_api_docker_virtualenv.py
@@ -17,6 +17,8 @@
# under the License.
from __future__ import annotations
+import contextlib
+
# [START tutorial]
# [START import_module]
import os
@@ -109,12 +111,10 @@ with models.DAG(
# The try/except here is because Airflow versions less than 2.2.0 doesn't support
# @task.docker decorator and we use this dag in CI test. Thus, in order not to
# break the CI test, we added this try/except here.
- try:
+ with contextlib.suppress(AttributeError):
# [START dag_invocation]
tutorial_dag = tutorial_taskflow_api_docker_virtualenv()
# [END dag_invocation]
- except AttributeError:
- pass
from tests.system.utils import get_test_run # noqa: E402
diff --git a/tests/test_utils/system_tests_class.py b/tests/test_utils/system_tests_class.py
index 7b07857ebe..857f43f61b 100644
--- a/tests/test_utils/system_tests_class.py
+++ b/tests/test_utils/system_tests_class.py
@@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations
+import contextlib
import logging
import os
import shutil
@@ -164,9 +165,7 @@ class SystemTest:
@staticmethod
def delete_dummy_file(filename, dir_path):
full_path = os.path.join(dir_path, filename)
- try:
+ with contextlib.suppress(FileNotFoundError):
os.remove(full_path)
- except FileNotFoundError:
- pass
if dir_path != "/tmp":
shutil.rmtree(dir_path, ignore_errors=True)