Skip to content

Commit 32d1874

Browse files
ref(hub): Create custom DeprecationWarning class for hubs
1 parent da15b63 commit 32d1874

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

Diff for: sentry_sdk/hub.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,29 @@ def overload(x):
5656
return x
5757

5858

59-
_HUB_DEPRECATION_MESSAGE = (
60-
"`sentry_sdk.Hub` is deprecated and will be removed in a future major release. "
61-
"Please consult our 1.x to 2.x migration guide for details on how to migrate "
62-
"`Hub` usage to the new API: "
63-
"https://docs.sentry.io/platforms/python/migration/1.x-to-2.x"
64-
)
59+
class SentryHubDeprecationWarning(DeprecationWarning):
60+
"""
61+
A custom deprecation warning to inform users that the Hub is deprecated.
62+
"""
63+
64+
_MESSAGE = (
65+
"`sentry_sdk.Hub` is deprecated and will be removed in a future major release. "
66+
"Please consult our 1.x to 2.x migration guide for details on how to migrate "
67+
"`Hub` usage to the new API: "
68+
"https://docs.sentry.io/platforms/python/migration/1.x-to-2.x"
69+
)
70+
71+
def __init__(self, *args):
72+
# type: () -> None
73+
super().__init__(self._MESSAGE, *args)
6574

6675

6776
@contextmanager
6877
def _suppress_hub_deprecation_warning():
6978
# type: () -> Generator[None, None, None]
7079
"""Utility function to suppress deprecation warnings for the Hub."""
7180
with warnings.catch_warnings():
72-
warnings.filterwarnings("ignore", _HUB_DEPRECATION_MESSAGE, DeprecationWarning)
81+
warnings.filterwarnings("ignore", category=SentryHubDeprecationWarning)
7382
yield
7483

7584

@@ -81,7 +90,7 @@ class HubMeta(type):
8190
def current(cls):
8291
# type: () -> Hub
8392
"""Returns the current instance of the hub."""
84-
warnings.warn(_HUB_DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
93+
warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
8594
rv = _local.get(None)
8695
if rv is None:
8796
with _suppress_hub_deprecation_warning():
@@ -94,7 +103,7 @@ def current(cls):
94103
def main(cls):
95104
# type: () -> Hub
96105
"""Returns the main instance of the hub."""
97-
warnings.warn(_HUB_DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
106+
warnings.warn(SentryHubDeprecationWarning, stacklevel=2)
98107
return GLOBAL_HUB
99108

100109

@@ -125,7 +134,7 @@ def __init__(
125134
scope=None, # type: Optional[Any]
126135
):
127136
# type: (...) -> None
128-
warnings.warn(_HUB_DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
137+
warnings.warn(SentryHubDeprecationWarning(), stacklevel=2)
129138

130139
current_scope = None
131140

Diff for: tests/test_basics.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -842,18 +842,18 @@ def test_last_event_id_scope(sentry_init):
842842

843843

844844
def test_hub_constructor_deprecation_warning():
845-
with pytest.warns(DeprecationWarning):
845+
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning):
846846
Hub()
847847

848848

849849
def test_hub_current_deprecation_warning():
850-
with pytest.warns(DeprecationWarning) as warning_records:
850+
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning) as warning_records:
851851
Hub.current
852852

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

856856

857857
def test_hub_main_deprecation_warnings():
858-
with pytest.warns(DeprecationWarning):
858+
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning):
859859
Hub.main

0 commit comments

Comments
 (0)