Skip to content

Commit e7218da

Browse files
feat(init): Remove sentry_sdk.init context manager
BREAKING CHANGE: `sentry_sdk.init` now returns `None` instead of a context manager.
1 parent fae6713 commit e7218da

File tree

3 files changed

+4
-61
lines changed

3 files changed

+4
-61
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Sentry SDK Migration Guide
22

3-
43
## Upgrading to 3.0
54

65
Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of what's changed. Looking for a more digestible summary? See the [guide in the docs](https://docs.sentry.io/platforms/python/migration/2.x-to-3.x) with the most common migration patterns.
@@ -19,6 +18,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
1918
- `sentry_sdk.continue_trace` no longer returns a `Transaction` and is now a context manager.
2019
- Redis integration: In Redis pipeline spans there is no `span["data"]["redis.commands"]` that contains a dict `{"count": 3, "first_ten": ["cmd1", "cmd2", ...]}` but instead `span["data"]["redis.commands.count"]` (containing `3`) and `span["data"]["redis.commands.first_ten"]` (containing `["cmd1", "cmd2", ...]`).
2120
- clickhouse-driver integration: The query is now available under the `db.query.text` span attribute (only if `send_default_pii` is `True`).
21+
- `sentry_sdk.init` now returns `None` instead of a context manager.
2222

2323
### Removed
2424

@@ -40,7 +40,6 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
4040
- `continue_from_headers`, `continue_from_environ` and `from_traceparent` have been removed, please use top-level API `sentry_sdk.continue_trace` instead.
4141
- `PropagationContext` constructor no longer takes a `dynamic_sampling_context` but takes a `baggage` object instead.
4242

43-
4443
### Deprecated
4544

4645
- `sentry_sdk.start_transaction` is deprecated. Use `sentry_sdk.start_span` instead.

sentry_sdk/_init_implementation.py

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,13 @@
1-
import warnings
2-
31
from typing import TYPE_CHECKING
42

53
import sentry_sdk
64

75
if TYPE_CHECKING:
8-
from typing import Any, ContextManager, Optional
6+
from typing import Any, Optional
97

108
import sentry_sdk.consts
119

1210

13-
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-
21-
def __init__(self, client):
22-
# type: (sentry_sdk.Client) -> None
23-
self._client = client
24-
25-
def __enter__(self):
26-
# type: () -> _InitGuard
27-
warnings.warn(
28-
self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE,
29-
stacklevel=2,
30-
category=DeprecationWarning,
31-
)
32-
33-
return self
34-
35-
def __exit__(self, exc_type, exc_value, tb):
36-
# type: (Any, Any, Any) -> None
37-
warnings.warn(
38-
self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE,
39-
stacklevel=2,
40-
category=DeprecationWarning,
41-
)
42-
43-
c = self._client
44-
if c is not None:
45-
c.close()
46-
47-
4811
def _check_python_deprecations():
4912
# type: () -> None
5013
# Since we're likely to deprecate Python versions in the future, I'm keeping
@@ -54,16 +17,14 @@ def _check_python_deprecations():
5417

5518

5619
def _init(*args, **kwargs):
57-
# type: (*Optional[str], **Any) -> ContextManager[Any]
20+
# type: (*Optional[str], **Any) -> None
5821
"""Initializes the SDK and optionally integrations.
5922
6023
This takes the same arguments as the client constructor.
6124
"""
6225
client = sentry_sdk.Client(*args, **kwargs)
6326
sentry_sdk.get_global_scope().set_client(client)
6427
_check_python_deprecations()
65-
rv = _InitGuard(client)
66-
return rv
6728

6829

6930
if TYPE_CHECKING:
@@ -73,7 +34,7 @@ def _init(*args, **kwargs):
7334
# Use `ClientConstructor` to define the argument types of `init` and
7435
# `ContextManager[Any]` to tell static analyzers about the return type.
7536

76-
class init(sentry_sdk.consts.ClientConstructor, _InitGuard): # noqa: N801
37+
class init(sentry_sdk.consts.ClientConstructor): # noqa: N801
7738
pass
7839

7940
else:

tests/test_api.py

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

4-
import sentry_sdk
54
from sentry_sdk import (
65
capture_exception,
76
continue_trace,
@@ -182,19 +181,3 @@ def test_set_tags(sentry_init, capture_events):
182181
"tag2": "updated",
183182
"tag3": "new",
184183
}, "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)