Repository: getsentry/sentry-python

Issue:
Deprecate `sentry_sdk.push_scope`
Calling `sentry_sdk.push_scope` should emit a `DeprecationWarning`. Add a test to verify this behavior

Discussion:

Patch:
diff --git a/sentry_sdk/api.py b/sentry_sdk/api.py
index d28dbd92..8476ac1e 100644
--- a/sentry_sdk/api.py
+++ b/sentry_sdk/api.py
@@ -238,9 +238,19 @@ def push_scope(  # noqa: F811
     :returns: If no `callback` is provided, a context manager that should
         be used to pop the scope again.
     """
+    warnings.warn(
+        "sentry_sdk.push_scope is deprecated and will be removed in the next major version. "
+        "Please consult our migration guide to learn how to migrate to the new API: "
+        "https://docs.sentry.io/platforms/python/migration/1.x-to-2.x#scope-pushing",
+        DeprecationWarning,
+        stacklevel=2,
+    )
+
     if callback is not None:
-        with push_scope() as scope:
-            callback(scope)
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore", DeprecationWarning)
+            with push_scope() as scope:
+                callback(scope)
         return None
 
     return _ScopeManager()


Test patch:
diff --git a/tests/test_api.py b/tests/test_api.py
index 1f2a1b78..d8db519e 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -12,6 +12,7 @@ from sentry_sdk import (
     start_transaction,
     set_tags,
     configure_scope,
+    push_scope,
 )
 
 from sentry_sdk.client import Client, NonRecordingClient
@@ -186,3 +187,9 @@ def test_configure_scope_deprecation():
     with pytest.warns(DeprecationWarning):
         with configure_scope():
             ...
+
+
+def test_push_scope_deprecation():
+    with pytest.warns(DeprecationWarning):
+        with push_scope():
+            ...
diff --git a/tests/test_basics.py b/tests/test_basics.py
index 0bec698a..022f44ed 100644
--- a/tests/test_basics.py
+++ b/tests/test_basics.py
@@ -295,7 +295,7 @@ def test_breadcrumb_arguments(sentry_init, capture_events):
     add_breadcrumb(crumb=dict(foo=42))
 
 
-def test_push_scope(sentry_init, capture_events):
+def test_push_scope(sentry_init, capture_events, suppress_deprecation_warnings):
     sentry_init()
     events = capture_events()
 
@@ -312,7 +312,9 @@ def test_push_scope(sentry_init, capture_events):
     assert "exception" in event
 
 
-def test_push_scope_null_client(sentry_init, capture_events):
+def test_push_scope_null_client(
+    sentry_init, capture_events, suppress_deprecation_warnings
+):
     """
     This test can be removed when we remove push_scope and the Hub from the SDK.
     """