Skip to content

Commit 84fc4fe

Browse files
committed
mypy and tests modification
1 parent 3a10d50 commit 84fc4fe

File tree

3 files changed

+26
-55
lines changed

3 files changed

+26
-55
lines changed

aws_lambda_powertools/utilities/data_masking/base.py

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import warnings
66
from copy import deepcopy
7-
from typing import TYPE_CHECKING, Any, Callable, Mapping, Sequence, overload
7+
from typing import TYPE_CHECKING, Any, Callable, Mapping, Sequence
88

99
from jsonpath_ng.ext import parse
1010

@@ -66,6 +66,10 @@ def encrypt(
6666
fields=None,
6767
action=self.provider.encrypt,
6868
provider_options=provider_options or {},
69+
dynamic_mask=None,
70+
custom_mask=None,
71+
regex_pattern=None,
72+
mask_format=None,
6973
**encryption_context,
7074
)
7175

@@ -80,47 +84,25 @@ def decrypt(
8084
fields=None,
8185
action=self.provider.decrypt,
8286
provider_options=provider_options or {},
87+
dynamic_mask=None,
88+
custom_mask=None,
89+
regex_pattern=None,
90+
mask_format=None,
8391
**encryption_context,
8492
)
8593

86-
@overload
87-
def erase(self, data, fields: None) -> str: ...
88-
89-
@overload
90-
def erase(self, data: list, fields: list[str]) -> list[str]: ...
91-
92-
@overload
93-
def erase(self, data: tuple, fields: list[str]) -> tuple[str]: ...
94-
95-
@overload
96-
def erase(self, data: dict, fields: list[str]) -> dict: ...
97-
98-
@overload
99-
def erase(self, data: dict[Any, Any], *, masking_rules: dict[str, object]) -> dict[Any, Any]: ...
100-
101-
@overload
102-
def erase(
103-
self,
104-
data: dict,
105-
fields: list[str],
106-
dynamic_mask: bool | None = None,
107-
custom_mask: str | None = None,
108-
regex_pattern: str | None = None,
109-
mask_format: str | None = None,
110-
) -> dict: ...
111-
11294
def erase(
11395
self,
114-
data: Sequence | Mapping,
96+
data: Any,
11597
fields: list[str] | None = None,
11698
dynamic_mask: bool | None = None,
11799
custom_mask: str | None = None,
118100
regex_pattern: str | None = None,
119101
mask_format: str | None = None,
120102
masking_rules: dict | None = None,
121-
) -> str | list[str] | tuple[str] | dict:
103+
) -> Any:
122104
if masking_rules:
123-
return self._apply_masking_rules(data, masking_rules)
105+
return self._apply_masking_rules(data=data, masking_rules=masking_rules)
124106
else:
125107
return self._apply_action(
126108
data=data,
@@ -142,8 +124,8 @@ def _apply_action(
142124
custom_mask: str | None = None,
143125
regex_pattern: str | None = None,
144126
mask_format: str | None = None,
145-
**encryption_context: str,
146-
):
127+
**kwargs: Any,
128+
) -> Any:
147129
"""
148130
Helper method to determine whether to apply a given action to the entire input data
149131
or to specific fields if the 'fields' argument is specified.
@@ -159,8 +141,6 @@ def _apply_action(
159141
and returns the modified value.
160142
provider_options : dict
161143
Provider specific keyword arguments to propagate; used as an escape hatch.
162-
encryption_context: str
163-
Encryption context to use in encrypt and decrypt operations.
164144
165145
Returns
166146
-------
@@ -179,7 +159,7 @@ def _apply_action(
179159
custom_mask=custom_mask,
180160
regex_pattern=regex_pattern,
181161
mask_format=mask_format,
182-
**encryption_context,
162+
**kwargs,
183163
)
184164
else:
185165
logger.debug(f"Running action {action.__name__} with the entire data")
@@ -190,7 +170,7 @@ def _apply_action(
190170
custom_mask=custom_mask,
191171
regex_pattern=regex_pattern,
192172
mask_format=mask_format,
193-
**encryption_context,
173+
**kwargs,
194174
)
195175

196176
def _apply_action_to_fields(

aws_lambda_powertools/utilities/data_masking/provider/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def encrypt(self, data) -> str:
2828
def decrypt(self, data) -> Any:
2929
# Implementation logic for data decryption
3030
31-
def erase(self, data) -> str | Iterable:
31+
def erase(self, data) -> Any | Iterable:
3232
# Implementation logic for data masking
3333
pass
3434
@@ -78,7 +78,7 @@ def erase(
7878
**kwargs,
7979
) -> Any:
8080

81-
result = DATA_MASKING_STRING
81+
result: Any = DATA_MASKING_STRING
8282

8383
if not any([dynamic_mask, custom_mask, regex_pattern, mask_format, masking_rules]):
8484
if isinstance(data, (str, int, float, dict, bytes)):

tests/unit/data_masking/_aws_encryption_sdk/test_unit_data_masking.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -256,33 +256,24 @@ def test_erase_json_dict_with_fields_and_masks(data_masker):
256256

257257
def test_erase_json_dict_with_complex_masking_rules(data_masker):
258258
# GIVEN the data type is a json representation of a dictionary with nested and filtered paths
259-
data = json.dumps(
260-
{
261-
"email": "[email protected]",
262-
"age": 30,
263-
"addres": [
264-
{"postcode": 13000, "street": "123 Main St", "details": {"name": "Home", "type": "Primary"}},
265-
{"postcode": 14000, "street": "456 Other Street", "details": {"name": "Office", "type": "Secondary"}},
266-
],
267-
},
268-
)
259+
data = {
260+
"email": "[email protected]",
261+
"age": 30,
262+
"address": {"zip": 13000, "street": "123 Main St", "details": {"name": "Home", "type": "Primary"}},
263+
}
269264

270265
# WHEN erase is called with complex masking rules
271266
masking_rules = {
272267
"email": {"regex_pattern": "(.)(.*)(@.*)", "mask_format": r"\1****\3"},
273268
"age": {"dynamic_mask": True},
274-
"addres..name": {"custom_mask": "xxx"},
275-
"addres[?(@.postcode > 12000)]": {"dynamic_mask": True},
269+
"address.zip": {"custom_mask": "xxx"},
276270
}
277271

278272
masked_json_string = data_masker.erase(data, masking_rules=masking_rules)
279273

280274
# THEN the result should have all specified fields masked according to their rules
281275
assert masked_json_string == {
282276
"email": "j****@example.com",
283-
"age": "*****",
284-
"addres": [
285-
{"postcode": "*****", "street": "*** *** **", "details": {"name": "xxx", "type": "*******"}},
286-
{"postcode": "*****", "street": "*** ***** ******", "details": {"name": "xxx", "type": "********"}},
287-
],
277+
"age": "**",
278+
"address": {"zip": "xxx", "street": "123 Main St", "details": {"name": "Home", "type": "Primary"}},
288279
}

0 commit comments

Comments
 (0)