Skip to content

Commit fae6713

Browse files
ref(init): Deprecate sentry_sdk.init context manager
It is possible to use the return value of `sentry_sdk.init` as a context manager; however, this functionality has not been maintained for a long time, and it does not seem to be documented anywhere. So, we are deprecating this functionality, and we will remove it in the next major release. Closes #3282
1 parent 8ee97d9 commit fae6713

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

sentry_sdk/_init_implementation.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
from typing import TYPE_CHECKING
24

35
import sentry_sdk
@@ -9,16 +11,35 @@
911

1012

1113
class _InitGuard:
14+
_CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE = (
15+
"Using the return value of sentry_sdk.init as a context manager "
16+
"and manually calling the __enter__ and __exit__ methods on the "
17+
"return value are deprecated. We are no longer maintaining this "
18+
"functionality, and we will remove it in the next major release."
19+
)
20+
1221
def __init__(self, client):
1322
# type: (sentry_sdk.Client) -> None
1423
self._client = client
1524

1625
def __enter__(self):
1726
# type: () -> _InitGuard
27+
warnings.warn(
28+
self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE,
29+
stacklevel=2,
30+
category=DeprecationWarning,
31+
)
32+
1833
return self
1934

2035
def __exit__(self, exc_type, exc_value, tb):
2136
# type: (Any, Any, Any) -> None
37+
warnings.warn(
38+
self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE,
39+
stacklevel=2,
40+
category=DeprecationWarning,
41+
)
42+
2243
c = self._client
2344
if c is not None:
2445
c.close()

tests/test_api.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from unittest import mock
33

4+
import sentry_sdk
45
from sentry_sdk import (
56
capture_exception,
67
continue_trace,
@@ -181,3 +182,19 @@ def test_set_tags(sentry_init, capture_events):
181182
"tag2": "updated",
182183
"tag3": "new",
183184
}, "Updating tags with empty dict changed tags"
185+
186+
187+
def test_init_context_manager_deprecation():
188+
with pytest.warns(DeprecationWarning):
189+
with sentry_sdk.init():
190+
...
191+
192+
193+
def test_init_enter_deprecation():
194+
with pytest.warns(DeprecationWarning):
195+
sentry_sdk.init().__enter__()
196+
197+
198+
def test_init_exit_deprecation():
199+
with pytest.warns(DeprecationWarning):
200+
sentry_sdk.init().__exit__(None, None, None)

0 commit comments

Comments
 (0)