Skip to content

Commit 37ba0fd

Browse files
committed
move check to within the method
1 parent 7848bc7 commit 37ba0fd

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

sentry_sdk/scope.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -890,13 +890,7 @@ def user(self, value: "Optional[Dict[str, Any]]") -> None:
890890

891891
def set_user(self, value: "Optional[Dict[str, Any]]") -> None:
892892
"""Sets a user for the scope."""
893-
if value is not None:
894-
self._user = {}
895-
for k, v in value.items():
896-
if v is not None:
897-
self._user[k] = v
898-
else:
899-
self._user = None
893+
self._user = value
900894

901895
session = self.get_isolation_scope()._session
902896
if session is not None:
@@ -1760,7 +1754,11 @@ def _apply_user_attributes_to_telemetry(
17601754
("user.email", "email"),
17611755
("user.ip_address", "ip_address"),
17621756
):
1763-
if user_attribute in self._user and attribute_name not in attributes:
1757+
if (
1758+
user_attribute in self._user
1759+
and attribute_name not in attributes
1760+
and self._user[user_attribute] is not None
1761+
):
17641762
attributes[attribute_name] = self._user[user_attribute]
17651763

17661764
def _drop(self, cause: "Any", ty: str) -> "Optional[Any]":

tests/test_scope.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,27 @@ def test_set_user(sentry_init, capture_events):
8282

8383

8484
def test_set_user_none_values_are_dropped_when_copying_to_attributes(
85-
sentry_init, capture_events
85+
sentry_init, capture_items
8686
):
87-
sentry_init()
88-
events = capture_events()
89-
90-
sentry_sdk.get_isolation_scope().set_user(
91-
{"email": "ada@beans.com", "username": None}
87+
sentry_init(
88+
traces_sample_rate=1.0,
89+
send_default_pii=True,
90+
_experiments={"trace_lifecycle": "stream"},
9291
)
93-
capture_exception(NameError())
94-
assert events[-1]["user"] == {"email": "ada@beans.com"}
92+
items = capture_items("span")
93+
94+
with sentry_sdk.traces.start_span(name="test_segment"):
95+
sentry_sdk.get_isolation_scope().set_user(
96+
{"email": "ada@beans.com", "username": None}
97+
)
98+
capture_exception(NameError())
99+
100+
sentry_sdk.flush()
101+
102+
segment = items[0].payload
103+
104+
assert "user.name" not in segment["attributes"]
105+
assert segment["attributes"]["user.email"] == "ada@beans.com"
95106

96107

97108
def test_merging(sentry_init, capture_events):

0 commit comments

Comments
 (0)