Skip to content

Commit 9bd7625

Browse files
tetienneiMicknl
andauthored
refactor(obfuscate): improve type consistency and simplify branch logic (#1890)
## Summary - Use sets for key membership checks (O(1) lookup vs O(n) for lists) - Simplify list iteration in `obfuscate_sensitive_data` by checking for dict directly instead of excluding other types - Remove obsolete pylint disable comment - Use tuple for value membership check since values can be unhashable types ## Test plan - [x] Existing tests pass - [x] Pre-commit checks pass --------- Co-authored-by: Mick Vleeshouwer <[email protected]>
1 parent 716ae71 commit 9bd7625

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

pyoverkiz/obfuscate.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ def obfuscate_string(input: str) -> str:
2424
return re.sub(r"[a-zA-Z0-9_.-]*", "*", str(input))
2525

2626

27-
# pylint: disable=too-many-branches
2827
def obfuscate_sensitive_data(data: dict[str, Any]) -> JSON:
2928
"""Mask Overkiz JSON data to remove sensitive data."""
3029
mask_next_value = False
3130

3231
for key, value in data.items():
33-
if key in ["gatewayId", "id", "deviceURL"]:
32+
if key in {"gatewayId", "id", "deviceURL"}:
3433
data[key] = obfuscate_id(value)
3534

36-
if key in [
35+
if key in {
3736
"label",
3837
"city",
3938
"country",
@@ -42,16 +41,16 @@ def obfuscate_sensitive_data(data: dict[str, Any]) -> JSON:
4241
"addressLine2",
4342
"longitude",
4443
"latitude",
45-
]:
44+
}:
4645
data[key] = obfuscate_string(value)
4746

48-
if value in [
47+
if value in (
4948
"core:NameState",
5049
"homekit:SetupCode",
5150
"homekit:SetupPayload",
5251
"core:SSIDState",
5352
"core:NetworkMacState",
54-
]:
53+
):
5554
mask_next_value = True
5655

5756
if mask_next_value and key == "value":
@@ -63,17 +62,7 @@ def obfuscate_sensitive_data(data: dict[str, Any]) -> JSON:
6362
obfuscate_sensitive_data(value)
6463
elif isinstance(value, list):
6564
for val in value:
66-
if isinstance(val, str):
67-
continue
68-
if isinstance(val, int):
69-
continue
70-
if isinstance(val, float):
71-
continue
72-
if isinstance(val, list):
73-
continue
74-
if val is None:
75-
continue
76-
77-
obfuscate_sensitive_data(val)
65+
if isinstance(val, dict):
66+
obfuscate_sensitive_data(val)
7867

7968
return data

0 commit comments

Comments
 (0)