Skip to content

Commit

Permalink
ref(hub): Create custom DeprecationWarning class for hubs
Browse files Browse the repository at this point in the history
  • Loading branch information
szokeasaurusrex committed Jul 17, 2024
1 parent da15b63 commit 69eb0f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
29 changes: 19 additions & 10 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,29 @@ def overload(x):
return x


_HUB_DEPRECATION_MESSAGE = (
"`sentry_sdk.Hub` is deprecated and will be removed in a future major release. "
"Please consult our 1.x to 2.x migration guide for details on how to migrate "
"`Hub` usage to the new API: "
"https://docs.sentry.io/platforms/python/migration/1.x-to-2.x"
)
class SentryHubDeprecationWarning(DeprecationWarning):
"""
A custom deprecation warning to inform users that the Hub is deprecated.
"""

_MESSAGE = (
"`sentry_sdk.Hub` is deprecated and will be removed in a future major release. "
"Please consult our 1.x to 2.x migration guide for details on how to migrate "
"`Hub` usage to the new API: "
"https://docs.sentry.io/platforms/python/migration/1.x-to-2.x"
)

def __init__(self, *_):
# type: (*object) -> None
super().__init__(self._MESSAGE)


@contextmanager
def _suppress_hub_deprecation_warning():
# type: () -> Generator[None, None, None]
"""Utility function to suppress deprecation warnings for the Hub."""
with warnings.catch_warnings():
warnings.filterwarnings("ignore", _HUB_DEPRECATION_MESSAGE, DeprecationWarning)
warnings.filterwarnings("ignore", category=SentryHubDeprecationWarning)
yield


Expand All @@ -81,7 +90,7 @@ class HubMeta(type):
def current(cls):
# type: () -> Hub
"""Returns the current instance of the hub."""
warnings.warn(_HUB_DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
rv = _local.get(None)
if rv is None:
with _suppress_hub_deprecation_warning():
Expand All @@ -94,7 +103,7 @@ def current(cls):
def main(cls):
# type: () -> Hub
"""Returns the main instance of the hub."""
warnings.warn(_HUB_DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
return GLOBAL_HUB


Expand Down Expand Up @@ -125,7 +134,7 @@ def __init__(
scope=None, # type: Optional[Any]
):
# type: (...) -> None
warnings.warn(_HUB_DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)

current_scope = None

Expand Down
6 changes: 3 additions & 3 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,18 +842,18 @@ def test_last_event_id_scope(sentry_init):


def test_hub_constructor_deprecation_warning():
with pytest.warns(DeprecationWarning):
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning):
Hub()


def test_hub_current_deprecation_warning():
with pytest.warns(DeprecationWarning) as warning_records:
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning) as warning_records:
Hub.current

# Make sure we only issue one deprecation warning
assert len(warning_records) == 1


def test_hub_main_deprecation_warnings():
with pytest.warns(DeprecationWarning):
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning):
Hub.main

0 comments on commit 69eb0f0

Please sign in to comment.