Skip to content

Commit b74c30e

Browse files
fix(api): push_scope deprecation warning
Although `push_scope` was meant to be deprecated since Sentry SDK 2.0.0, calling `push_scope` did not raise a deprecation warning. Now, it does. Fixes #3347
1 parent d09b113 commit b74c30e

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

sentry_sdk/api.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,19 @@ def push_scope( # noqa: F811
238238
:returns: If no `callback` is provided, a context manager that should
239239
be used to pop the scope again.
240240
"""
241+
warnings.warn(
242+
"sentry_sdk.push_scope is deprecated and will be removed in the next major version. "
243+
"Please consult our migration guide to learn how to migrate to the new API: "
244+
"https://docs.sentry.io/platforms/python/migration/1.x-to-2.x#scope-pushing",
245+
DeprecationWarning,
246+
stacklevel=2,
247+
)
248+
241249
if callback is not None:
242-
with push_scope() as scope:
243-
callback(scope)
250+
with warnings.catch_warnings():
251+
warnings.simplefilter("ignore", DeprecationWarning)
252+
with push_scope() as scope:
253+
callback(scope)
244254
return None
245255

246256
return _ScopeManager()

tests/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
start_transaction,
1313
set_tags,
1414
configure_scope,
15+
push_scope,
1516
)
1617

1718
from sentry_sdk.client import Client, NonRecordingClient
@@ -186,3 +187,9 @@ def test_configure_scope_deprecation():
186187
with pytest.warns(DeprecationWarning):
187188
with configure_scope():
188189
...
190+
191+
192+
def test_push_scope_deprecation():
193+
with pytest.warns(DeprecationWarning):
194+
with push_scope():
195+
...

tests/test_basics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def before_breadcrumb(crumb, hint):
295295
add_breadcrumb(crumb=dict(foo=42))
296296

297297

298-
def test_push_scope(sentry_init, capture_events):
298+
def test_push_scope(sentry_init, capture_events, suppress_deprecation_warnings):
299299
sentry_init()
300300
events = capture_events()
301301

@@ -312,7 +312,9 @@ def test_push_scope(sentry_init, capture_events):
312312
assert "exception" in event
313313

314314

315-
def test_push_scope_null_client(sentry_init, capture_events):
315+
def test_push_scope_null_client(
316+
sentry_init, capture_events, suppress_deprecation_warnings
317+
):
316318
"""
317319
This test can be removed when we remove push_scope and the Hub from the SDK.
318320
"""

0 commit comments

Comments
 (0)