From fab43b24786c1418ce58903da7dc9df10eacadbb Mon Sep 17 00:00:00 2001 From: Andrey Anshin Date: Thu, 28 Mar 2024 02:12:37 +0400 Subject: [PATCH 1/2] Undeprecating `DBApiHookForTests._make_common_data_structure` --- airflow/providers/common/sql/hooks/sql.py | 17 +++++++-------- tests/providers/common/sql/hooks/test_sql.py | 22 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/airflow/providers/common/sql/hooks/sql.py b/airflow/providers/common/sql/hooks/sql.py index 3b9e6fba1e63af..7c2480bb6af52d 100644 --- a/airflow/providers/common/sql/hooks/sql.py +++ b/airflow/providers/common/sql/hooks/sql.py @@ -17,6 +17,7 @@ from __future__ import annotations import contextlib +import warnings from contextlib import closing from datetime import datetime from typing import ( @@ -36,7 +37,6 @@ from urllib.parse import urlparse import sqlparse -from deprecated import deprecated from more_itertools import chunked from sqlalchemy import create_engine @@ -422,14 +422,6 @@ def run( else: return results - @deprecated( - reason=( - "The `_make_serializable` method is deprecated and support will be removed in a future " - "version of the common.sql provider. Please update the DbApiHook's provider " - "to a version based on common.sql >= 1.9.1." - ), - category=AirflowProviderDeprecationWarning, - ) def _make_common_data_structure(self, result: T | Sequence[T]) -> tuple | list[tuple]: """Ensure the data returned from an SQL command is a standard tuple or list[tuple]. @@ -444,6 +436,13 @@ def _make_common_data_structure(self, result: T | Sequence[T]) -> tuple | list[t # Back-compatibility call for providers implementing old ยด_make_serializable' method. with contextlib.suppress(AttributeError): result = self._make_serializable(result=result) # type: ignore[attr-defined] + warnings.warn( + "The `_make_serializable` method is deprecated and support will be removed in a future " + f"version of the common.sql provider. Please update the {self.__class__.__name__}'s provider " + "to a version based on common.sql >= 1.9.1.", + AirflowProviderDeprecationWarning, + stacklevel=2, + ) if isinstance(result, Sequence): return cast(List[tuple], result) diff --git a/tests/providers/common/sql/hooks/test_sql.py b/tests/providers/common/sql/hooks/test_sql.py index eea912340241a1..438277ba4d6389 100644 --- a/tests/providers/common/sql/hooks/test_sql.py +++ b/tests/providers/common/sql/hooks/test_sql.py @@ -18,10 +18,13 @@ # from __future__ import annotations +import warnings +from typing import Any from unittest.mock import MagicMock import pytest +from airflow.exceptions import AirflowProviderDeprecationWarning from airflow.models import Connection from airflow.providers.common.sql.hooks.sql import DbApiHook, fetch_all_handler from airflow.utils.session import provide_session @@ -226,3 +229,22 @@ def test_no_query(empty_statement): with pytest.raises(ValueError) as err: dbapi_hook.run(sql=empty_statement) assert err.value.args[0] == "List of SQL statements is empty" + + +def test_make_common_data_structure_hook_has_deprecated_method(): + """If hook implements ``_make_serializable`` warning should be raised on call.""" + + class DBApiHookForMakeSerializableTests(DBApiHookForTests): + def _make_serializable(self, result: Any): + return result + + hook = DBApiHookForMakeSerializableTests() + with pytest.warns(AirflowProviderDeprecationWarning, match="`_make_serializable` method is deprecated"): + hook._make_common_data_structure(["foo", "bar", "baz"]) + + +def test_make_common_data_structure_no_deprecated_method(): + """If hook not implements ``_make_serializable`` there is no warning should be raised on call.""" + with warnings.catch_warnings(): + warnings.simplefilter("error", AirflowProviderDeprecationWarning) + DBApiHookForTests()._make_common_data_structure(["foo", "bar", "baz"]) From fb6dc351cf736deb24efa086ad53f5557475eb52 Mon Sep 17 00:00:00 2001 From: Andrey Anshin Date: Thu, 28 Mar 2024 11:47:20 +0400 Subject: [PATCH 2/2] Add missing db_test marker --- tests/providers/common/sql/hooks/test_sql.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/providers/common/sql/hooks/test_sql.py b/tests/providers/common/sql/hooks/test_sql.py index 438277ba4d6389..e0a7b0fccb0237 100644 --- a/tests/providers/common/sql/hooks/test_sql.py +++ b/tests/providers/common/sql/hooks/test_sql.py @@ -231,6 +231,7 @@ def test_no_query(empty_statement): assert err.value.args[0] == "List of SQL statements is empty" +@pytest.mark.db_test def test_make_common_data_structure_hook_has_deprecated_method(): """If hook implements ``_make_serializable`` warning should be raised on call.""" @@ -243,6 +244,7 @@ def _make_serializable(self, result: Any): hook._make_common_data_structure(["foo", "bar", "baz"]) +@pytest.mark.db_test def test_make_common_data_structure_no_deprecated_method(): """If hook not implements ``_make_serializable`` there is no warning should be raised on call.""" with warnings.catch_warnings():