Skip to content

refactor(data_masking): use standard collections for types #6493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion aws_lambda_powertools/utilities/data_masking/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
import warnings
from copy import deepcopy
from typing import TYPE_CHECKING, Any, Callable, Mapping, Sequence
from typing import TYPE_CHECKING, Any

from jsonpath_ng.ext import parse

Expand All @@ -23,6 +23,7 @@
from aws_lambda_powertools.warnings import PowertoolsUserWarning

if TYPE_CHECKING:
from collections.abc import Callable, Mapping, Sequence
from numbers import Number

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import functools
import json
import re
from typing import Any, Callable
from typing import TYPE_CHECKING, Any

from aws_lambda_powertools.utilities.data_masking.constants import DATA_MASKING_STRING

if TYPE_CHECKING:
from collections.abc import Callable

PRESERVE_CHARS = set("-_. ")
_regex_cache = {}

JSON_DUMPS_CALL = functools.partial(json.dumps, ensure_ascii=False)


class BaseProvider:
"""
Expand Down Expand Up @@ -49,7 +54,7 @@ def lambda_handler(event, context):

def __init__(
self,
json_serializer: Callable[..., str] = functools.partial(json.dumps, ensure_ascii=False),
json_serializer: Callable[..., str] = JSON_DUMPS_CALL,
json_deserializer: Callable[[str], Any] = json.loads,
) -> None:
self.json_serializer = json_serializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import logging
from binascii import Error
from typing import Any, Callable
from typing import TYPE_CHECKING, Any

import botocore
from aws_encryption_sdk import (
Expand Down Expand Up @@ -41,8 +41,13 @@
)
from aws_lambda_powertools.utilities.data_masking.provider import BaseProvider

if TYPE_CHECKING:
from collections.abc import Callable

logger = logging.getLogger(__name__)

JSON_DUMPS_CALL = functools.partial(json.dumps, ensure_ascii=False)


class AWSEncryptionSDKProvider(BaseProvider):
"""
Expand Down Expand Up @@ -81,7 +86,7 @@ def __init__(
max_cache_age_seconds: float = MAX_CACHE_AGE_SECONDS,
max_messages_encrypted: int = MAX_MESSAGES_ENCRYPTED,
max_bytes_encrypted: int = MAX_BYTES_ENCRYPTED,
json_serializer: Callable[..., str] = functools.partial(json.dumps, ensure_ascii=False),
json_serializer: Callable[..., str] = JSON_DUMPS_CALL,
json_deserializer: Callable[[str], Any] = json.loads,
):
super().__init__(json_serializer=json_serializer, json_deserializer=json_deserializer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import base64
import functools
import json
from typing import Any, Callable, Union
from typing import TYPE_CHECKING, Any

import pytest
from aws_encryption_sdk.identifiers import Algorithm
Expand All @@ -13,16 +15,21 @@
AWSEncryptionSDKProvider,
)

if TYPE_CHECKING:
from collections.abc import Callable

JSON_DUMPS_CALL = functools.partial(json.dumps, ensure_ascii=False)


class FakeEncryptionKeyProvider(BaseProvider):
def __init__(
self,
json_serializer: Callable = functools.partial(json.dumps, ensure_ascii=False),
json_serializer: Callable = JSON_DUMPS_CALL,
json_deserializer: Callable = json.loads,
) -> None:
super().__init__(json_serializer, json_deserializer)

def encrypt(self, data: Union[bytes, str], **kwargs) -> str:
def encrypt(self, data: bytes | str, **kwargs) -> str:
encoded_data: str = self.json_serializer(data)
ciphertext = base64.b64encode(encoded_data.encode("utf-8")).decode()
return ciphertext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import dataclasses

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/data_masking/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pytest_socket import disable_socket


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json

import pytest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

from aws_lambda_powertools.utilities.data_masking.exceptions import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

from aws_lambda_powertools.utilities.data_masking.base import DataMasking
Expand Down
Loading