Skip to content

Commit 461bd59

Browse files
ref: Improve scrub_dict typing (#2768)
This change improves the typing of the scrub_dict method. Previously, the scrub_dict method's type hints indicated that only dict[str, Any] was accepted as the parameter. However, the method is actually implemented to accept any object, since it checks the types of the parameters at runtime. Therefore, object is a more appropriate type hint for the parameter. #2753 depends on this change for mypy to pass
1 parent 1b0e932 commit 461bd59

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

sentry_sdk/scrubber.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
try:
2+
from typing import cast
3+
except ImportError:
4+
cast = lambda _, obj: obj
5+
16
from sentry_sdk.utils import (
27
capture_internal_exceptions,
38
AnnotatedValue,
@@ -8,8 +13,6 @@
813

914
if TYPE_CHECKING:
1015
from sentry_sdk._types import Event
11-
from typing import Any
12-
from typing import Dict
1316
from typing import List
1417
from typing import Optional
1518

@@ -66,7 +69,7 @@ def __init__(self, denylist=None, recursive=False):
6669
self.recursive = recursive
6770

6871
def scrub_list(self, lst):
69-
# type: (List[Any]) -> None
72+
# type: (object) -> None
7073
"""
7174
If a list is passed to this method, the method recursively searches the list and any
7275
nested lists for any dictionaries. The method calls scrub_dict on all dictionaries
@@ -77,24 +80,28 @@ def scrub_list(self, lst):
7780
return
7881

7982
for v in lst:
80-
if isinstance(v, dict):
81-
self.scrub_dict(v)
82-
elif isinstance(v, list):
83-
self.scrub_list(v)
83+
self.scrub_dict(v) # no-op unless v is a dict
84+
self.scrub_list(v) # no-op unless v is a list
8485

8586
def scrub_dict(self, d):
86-
# type: (Dict[str, Any]) -> None
87+
# type: (object) -> None
88+
"""
89+
If a dictionary is passed to this method, the method scrubs the dictionary of any
90+
sensitive data. The method calls itself recursively on any nested dictionaries (
91+
including dictionaries nested in lists) if self.recursive is True.
92+
This method does nothing if the parameter passed to it is not a dictionary.
93+
"""
8794
if not isinstance(d, dict):
8895
return
8996

9097
for k, v in d.items():
91-
if isinstance(k, string_types) and k.lower() in self.denylist:
98+
# The cast is needed because mypy is not smart enough to figure out that k must be a
99+
# string after the isinstance check.
100+
if isinstance(k, string_types) and cast(str, k).lower() in self.denylist:
92101
d[k] = AnnotatedValue.substituted_because_contains_sensitive_data()
93102
elif self.recursive:
94-
if isinstance(v, dict):
95-
self.scrub_dict(v)
96-
elif isinstance(v, list):
97-
self.scrub_list(v)
103+
self.scrub_dict(v) # no-op unless v is a dict
104+
self.scrub_list(v) # no-op unless v is a list
98105

99106
def scrub_request(self, event):
100107
# type: (Event) -> None

0 commit comments

Comments
 (0)