8000 ref(init): Deprecate `sentry_sdk.init` context manager · getsentry/sentry-python@eba2763 · GitHub
[go: up one dir, main page]

Skip to content

Commit eba2763

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 200d0cd commit eba2763

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,
@@ -195,3 +196,19 @@ def test_push_scope_deprecation():
195196
with pytest.warns(DeprecationWarning):
196197
with push_scope():
197198
...
199+
200+
201+
def test_init_context_manager_deprecation():
202+
with pytest.warns(DeprecationWarning):
203+
with sentry_sdk.init():
204+
...
205+
206+
207+
def test_init_enter_deprecation():
208+
with pytest.warns(DeprecationWarning):
209+
sentry_sdk.init().__enter__()
210+
211+
212+
def test_init_exit_deprecation():
213+
with pytest.warns(DeprecationWarning):
214+
sentry_sdk.init().__exit__(None, None, None)

0 commit comments

Comments
 (0)
0