Skip to content
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

[Enterprise Usage] Unable to assign generic PII transformers #676

Merged
merged 2 commits into from
Aug 2, 2023
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
17 changes: 17 additions & 0 deletions rdt/transformers/pii/anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ def __init__(self, provider_name=None, function_name=None, function_kwargs=None,
self.missing_value_generation = missing_value_generation
self._nan_frequency = 0.0

@classmethod
def get_supported_sdtypes(cls):
"""Return the supported sdtypes by the transformer.

Returns:
list:
Accepted input sdtypes of the transformer.
"""
unsupported_sdtypes = {'numerical', 'datetime', 'categorical', 'boolean', 'text', None}
all_sdtypes = {cls.INPUT_SDTYPE}
for transformer in BaseTransformer.get_subclasses():
if not issubclass(transformer, cls):
all_sdtypes.update(transformer.get_supported_sdtypes())

supported_sdtypes = all_sdtypes - unsupported_sdtypes
return list(supported_sdtypes)

def reset_randomization(self):
"""Create a new ``Faker`` instance."""
super().reset_randomization()
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/transformers/pii/test_anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def test_anonymizedfaker():
assert len(reverse_transform['username']) == 5


def test_anonymizedfaker_get_supported_sdtypes():
"""Test that the correct supported sdtypes are returned."""
# Run
supported_sdtypes = AnonymizedFaker.get_supported_sdtypes()

# Assert
assert supported_sdtypes == ['pii']


def test_anonymizedfaker_custom_provider():
"""End to end test with a custom provider and function for the ``AnonymizedFaker``."""
data = pd.DataFrame({
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/transformers/pii/test_anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,42 @@ def test___init__no_function_name(self):
with pytest.raises(TransformerInputError, match=expected_message):
AnonymizedFaker(provider_name='credit_card', locales=['en_US', 'fr_FR'])

@patch('rdt.transformers.pii.anonymizer.issubclass')
@patch('rdt.transformers.pii.anonymizer.BaseTransformer')
def test_get_supported_sdtypes(self, base_mock, issubclass_mock):
"""Test that the method returns all sdtypes except the basic ones."""
# Setup
issubclass_mock.return_value = False
numerical_mock = Mock()
numerical_mock.get_supported_sdtypes.return_value = ['numerical']
categorical_mock = Mock()
categorical_mock.get_supported_sdtypes.return_value = ['categorical']
datetime_mock = Mock()
datetime_mock.get_supported_sdtypes.return_value = ['datetime']
boolean_mock = Mock()
boolean_mock.get_supported_sdtypes.return_value = ['boolean', 'categorical']
text_mock = Mock()
text_mock.get_supported_sdtypes.return_value = ['text']
phone_mock = Mock()
phone_mock.get_supported_sdtypes.return_value = ['phone_number']
pii_mock = Mock()
pii_mock.get_supported_sdtypes.return_value = ['pii']
base_mock.get_subclasses.return_value = [
numerical_mock,
categorical_mock,
datetime_mock,
boolean_mock,
text_mock,
phone_mock,
pii_mock
]

# Run
supported_sdtypes = AnonymizedFaker.get_supported_sdtypes()

# Assert
assert sorted(supported_sdtypes) == sorted(['phone_number', 'pii'])

@patch('rdt.transformers.pii.anonymizer.BaseTransformer.reset_randomization')
@patch('rdt.transformers.pii.anonymizer.faker')
def test_reset_randomization(self, mock_faker, mock_base_reset):
Expand Down
Loading