Skip to content

Commit

Permalink
ref: should_send_default_pii shortcut (#2844)
Browse files Browse the repository at this point in the history
Currently, with the new scope API, calls to hub._should_send_default_pii need to be replaced with calls to sentry_sdk.get_client().should_send_default_pii.

This PR introduces scope.should_send_default_pii as a drop-in replacement for hub._should_send_default_pii, so we don't need to type out sentry_sdk.get_client().should_send_default_pii everywhere we need to check should_send_default_pii.
  • Loading branch information
szokeasaurusrex authored Mar 19, 2024
1 parent 553045b commit a046901
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def overload(x):

def _should_send_default_pii():
# type: () -> bool
# TODO: Migrate existing code to client.should_send_default_pii() and remove this function.
# TODO: Migrate existing code to `scope.should_send_default_pii()` and remove this function.
# New code should not use this function!
client = Hub.current.client
if not client:
Expand Down
6 changes: 6 additions & 0 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,5 +1658,11 @@ def use_isolation_scope(isolation_scope):
_isolation_scope.reset(isolation_token)


def should_send_default_pii():
# type: () -> bool
"""Shortcut for `Scope.get_client().should_send_default_pii()`."""
return Scope.get_client().should_send_default_pii()


# Circular imports
from sentry_sdk.client import NonRecordingClient
20 changes: 19 additions & 1 deletion tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
new_scope,
)
from sentry_sdk.client import Client, NonRecordingClient
from sentry_sdk.scope import Scope, ScopeType, use_isolation_scope, use_scope
from sentry_sdk.scope import (
Scope,
ScopeType,
use_isolation_scope,
use_scope,
should_send_default_pii,
)


def test_copying():
Expand Down Expand Up @@ -778,3 +784,15 @@ def test_nested_scopes_with_tags(sentry_init, capture_envelopes):
assert transaction["tags"] == {"isolation_scope1": 1, "current_scope2": 1, "trx": 1}
assert transaction["spans"][0]["tags"] == {"a": 1}
assert transaction["spans"][1]["tags"] == {"b": 1}


def test_should_send_default_pii_true(sentry_init):
sentry_init(send_default_pii=True)

assert should_send_default_pii() is True


def test_should_send_default_pii_false(sentry_init):
sentry_init(send_default_pii=False)

assert should_send_default_pii() is False

0 comments on commit a046901

Please sign in to comment.