diff --git a/airflow/providers/jdbc/hooks/jdbc.py b/airflow/providers/jdbc/hooks/jdbc.py index 832c16b9aad48..68123e526b1df 100644 --- a/airflow/providers/jdbc/hooks/jdbc.py +++ b/airflow/providers/jdbc/hooks/jdbc.py @@ -36,7 +36,11 @@ def suppress_and_warn(*exceptions: type[BaseException]): try: yield except exceptions as e: - warnings.warn(f"Exception suppressed: {e}\n{traceback.format_exc()}", category=UserWarning) + warnings.warn( + f"Exception suppressed: {e}\n{traceback.format_exc()}", + category=UserWarning, + stacklevel=3, + ) class JdbcHook(DbApiHook): diff --git a/pyproject.toml b/pyproject.toml index 0110cf4af9170..6fd468aa507b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -387,7 +387,6 @@ combine-as-imports = true "tests/providers/snowflake/operators/test_snowflake_sql.py" = ["E402"] # All the modules which do not follow B028 yet: https://docs.astral.sh/ruff/rules/no-explicit-stacklevel/ -"airflow/providers/jdbc/hooks/jdbc.py" = ["B028"] "helm_tests/airflow_aux/test_basic_helm_chart.py" = ["B028"] # https://github.com/apache/airflow/issues/39252 diff --git a/tests/providers/jdbc/hooks/test_jdbc.py b/tests/providers/jdbc/hooks/test_jdbc.py index 80eedf8ee47be..6e4387ee1ad10 100644 --- a/tests/providers/jdbc/hooks/test_jdbc.py +++ b/tests/providers/jdbc/hooks/test_jdbc.py @@ -25,11 +25,9 @@ import jaydebeapi import pytest -from airflow.exceptions import DeserializingResultError from airflow.models import Connection from airflow.providers.jdbc.hooks.jdbc import JdbcHook, suppress_and_warn from airflow.utils import db -from airflow.utils.context import AirflowContextDeprecationWarning pytestmark = pytest.mark.db_test @@ -180,10 +178,11 @@ def test_driver_extra_raises_warning_and_returns_default_driver_by_default(self, assert driver_class == "Blah driver class" def test_suppress_and_warn_when_raised_exception_is_suppressed(self): - with suppress_and_warn(AirflowContextDeprecationWarning): - raise AirflowContextDeprecationWarning() + with pytest.warns(UserWarning, match="Exception suppressed: Foo Bar"): + with suppress_and_warn(RuntimeError): + raise RuntimeError("Foo Bar") def test_suppress_and_warn_when_raised_exception_is_not_suppressed(self): - with pytest.raises(AirflowContextDeprecationWarning): - with suppress_and_warn(DeserializingResultError): - raise AirflowContextDeprecationWarning() + with pytest.raises(RuntimeError, match="Spam Egg"): + with suppress_and_warn(KeyError): + raise RuntimeError("Spam Egg")