Skip to content

Commit b06f401

Browse files
authored
Undeprecating DBApiHookForTests._make_common_data_structure (#38573)
* Undeprecating `DBApiHookForTests._make_common_data_structure` * Add missing db_test marker
1 parent d4c2ea4 commit b06f401

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

airflow/providers/common/sql/hooks/sql.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import annotations
1818

1919
import contextlib
20+
import warnings
2021
from contextlib import closing
2122
from datetime import datetime
2223
from typing import (
@@ -36,7 +37,6 @@
3637
from urllib.parse import urlparse
3738

3839
import sqlparse
39-
from deprecated import deprecated
4040
from more_itertools import chunked
4141
from sqlalchemy import create_engine
4242

@@ -422,14 +422,6 @@ def run(
422422
else:
423423
return results
424424

425-
@deprecated(
426-
reason=(
427-
"The `_make_serializable` method is deprecated and support will be removed in a future "
428-
"version of the common.sql provider. Please update the DbApiHook's provider "
429-
"to a version based on common.sql >= 1.9.1."
430-
),
431-
category=AirflowProviderDeprecationWarning,
432-
)
433425
def _make_common_data_structure(self, result: T | Sequence[T]) -> tuple | list[tuple]:
434426
"""Ensure the data returned from an SQL command is a standard tuple or list[tuple].
435427
@@ -444,6 +436,13 @@ def _make_common_data_structure(self, result: T | Sequence[T]) -> tuple | list[t
444436
# Back-compatibility call for providers implementing old ´_make_serializable' method.
445437
with contextlib.suppress(AttributeError):
446438
result = self._make_serializable(result=result) # type: ignore[attr-defined]
439+
warnings.warn(
440+
"The `_make_serializable` method is deprecated and support will be removed in a future "
441+
f"version of the common.sql provider. Please update the {self.__class__.__name__}'s provider "
442+
"to a version based on common.sql >= 1.9.1.",
443+
AirflowProviderDeprecationWarning,
444+
stacklevel=2,
445+
)
447446

448447
if isinstance(result, Sequence):
449448
return cast(List[tuple], result)

tests/providers/common/sql/hooks/test_sql.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
#
1919
from __future__ import annotations
2020

21+
import warnings
22+
from typing import Any
2123
from unittest.mock import MagicMock
2224

2325
import pytest
2426

27+
from airflow.exceptions import AirflowProviderDeprecationWarning
2528
from airflow.models import Connection
2629
from airflow.providers.common.sql.hooks.sql import DbApiHook, fetch_all_handler
2730
from airflow.utils.session import provide_session
@@ -226,3 +229,24 @@ def test_no_query(empty_statement):
226229
with pytest.raises(ValueError) as err:
227230
dbapi_hook.run(sql=empty_statement)
228231
assert err.value.args[0] == "List of SQL statements is empty"
232+
233+
234+
@pytest.mark.db_test
235+
def test_make_common_data_structure_hook_has_deprecated_method():
236+
"""If hook implements ``_make_serializable`` warning should be raised on call."""
237+
238+
class DBApiHookForMakeSerializableTests(DBApiHookForTests):
239+
def _make_serializable(self, result: Any):
240+
return result
241+
242+
hook = DBApiHookForMakeSerializableTests()
243+
with pytest.warns(AirflowProviderDeprecationWarning, match="`_make_serializable` method is deprecated"):
244+
hook._make_common_data_structure(["foo", "bar", "baz"])
245+
246+
247+
@pytest.mark.db_test
248+
def test_make_common_data_structure_no_deprecated_method():
249+
"""If hook not implements ``_make_serializable`` there is no warning should be raised on call."""
250+
with warnings.catch_warnings():
251+
warnings.simplefilter("error", AirflowProviderDeprecationWarning)
252+
DBApiHookForTests()._make_common_data_structure(["foo", "bar", "baz"])

0 commit comments

Comments
 (0)