Repository: getsentry/sentry-python

Issue:
Deprecate `init` context manager
See below:

> `init` is generally one per process anyway. In case people want DSN isolation, multiple clients is the way to go now. So I really don't see the use case for this. 
> Let's deprecate and remove.

_Originally posted by @sl0thentr0py in https://github.com/getsentry/sentry-python/pull/3276#discussion_r1675776684_
            

Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/getsentry/sentry-python/pull/3729?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 83.30%. Comparing base [(`5badf77`)](https://app.codecov.io/gh/getsentry/sentry-python/commit/5badf77444a07512bbd30ceef78f75993be6ada8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) to head [(`399c344`)](https://app.codecov.io/gh/getsentry/sentry-python/commit/399c344bbd60d56ae7345d7fe2fbc3e78b9f3158?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry).

:white_check_mark: All tests successful. No failed tests found.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #3729      +/-   ##
==========================================
- Coverage   84.37%   83.30%   -1.07%     
==========================================
  Files         136      136              
  Lines       14367    14371       +4     
  Branches     2417     2417              
==========================================
- Hits        12122    11972     -150     
- Misses       1495     1651     +156     
+ Partials      750      748       -2     
```

| [Files with missing lines](https://app.codecov.io/gh/getsentry/sentry-python/pull/3729?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | Coverage Δ | |
|---|---|---|
| [sentry\_sdk/\_init\_implementation.py](https://app.codecov.io/gh/getsentry/sentry-python/pull/3729?src=pr&el=tree&filepath=sentry_sdk%2F_init_implementation.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry#diff-c2VudHJ5X3Nkay9faW5pdF9pbXBsZW1lbnRhdGlvbi5weQ==) | `95.83% <100.00%> (+15.83%)` | :arrow_up: |

... and [18 files with indirect coverage changes](https://app.codecov.io/gh/getsentry/sentry-python/pull/3729/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry)

</details>

Patch:
diff --git a/sentry_sdk/_init_implementation.py b/sentry_sdk/_init_implementation.py
index 256a69ee..eb02b3d1 100644
--- a/sentry_sdk/_init_implementation.py
+++ b/sentry_sdk/_init_implementation.py
@@ -1,3 +1,5 @@
+import warnings
+
 from typing import TYPE_CHECKING
 
 import sentry_sdk
@@ -9,16 +11,35 @@ if TYPE_CHECKING:
 
 
 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()


Test patch:
diff --git a/tests/test_api.py b/tests/test_api.py
index ae194af7..3b2a9c8f 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -1,6 +1,7 @@
 import pytest
 from unittest import mock
 
+import sentry_sdk
 from sentry_sdk import (
     capture_exception,
     continue_trace,
@@ -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)