Skip to content

Commit

Permalink
fix(api): push_scope deprecation warning
Browse files Browse the repository at this point in the history
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
  • Loading branch information
szokeasaurusrex committed Jul 26, 2024
1 parent d09b113 commit b74c30e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
14 changes: 12 additions & 2 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
start_transaction,
set_tags,
configure_scope,
push_scope,
)

from sentry_sdk.client import Client, NonRecordingClient
Expand Down Expand Up @@ -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():
...
6 changes: 4 additions & 2 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def before_breadcrumb(crumb, hint):
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()

Expand All @@ -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.
"""
Expand Down

0 comments on commit b74c30e

Please sign in to comment.