Skip to content

ref(init): Deprecate sentry_sdk.init context manager #3729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading