Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stacklevel into the suppress_and_warn warning #39263

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion airflow/providers/jdbc/hooks/jdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions tests/providers/jdbc/hooks/test_jdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")