Skip to content

Commit

Permalink
ref(init): Deprecate sentry_sdk.init context manager (#3729)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
szokeasaurusrex authored Nov 12, 2024
1 parent 200d0cd commit d424226
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions sentry_sdk/_init_implementation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from typing import TYPE_CHECKING

import sentry_sdk
Expand All @@ -9,16 +11,35 @@


class _InitGuard:
_CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE = (
"Using the return value of sentry_sdk.init as a context manager "
"and manually calling the __enter__ and __exit__ methods on the "
"return value are deprecated. We are no longer maintaining this "
"functionality, and we will remove it in the next major release."
)

def __init__(self, client):
# type: (sentry_sdk.Client) -> None
self._client = client

def __enter__(self):
# type: () -> _InitGuard
warnings.warn(
self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE,
stacklevel=2,
category=DeprecationWarning,
)

return self

def __exit__(self, exc_type, exc_value, tb):
# type: (Any, Any, Any) -> None
warnings.warn(
self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE,
stacklevel=2,
category=DeprecationWarning,
)

c = self._client
if c is not None:
c.close()
Expand Down
17 changes: 17 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from unittest import mock

import sentry_sdk
from sentry_sdk import (
capture_exception,
continue_trace,
Expand Down Expand Up @@ -195,3 +196,19 @@ def test_push_scope_deprecation():
with pytest.warns(DeprecationWarning):
with push_scope():
...


def test_init_context_manager_deprecation():
with pytest.warns(DeprecationWarning):
with sentry_sdk.init():
...


def test_init_enter_deprecation():
with pytest.warns(DeprecationWarning):
sentry_sdk.init().__enter__()


def test_init_exit_deprecation():
with pytest.warns(DeprecationWarning):
sentry_sdk.init().__exit__(None, None, None)

0 comments on commit d424226

Please sign in to comment.