Skip to content

Commit

Permalink
Fix N818, E721 (#1382)
Browse files Browse the repository at this point in the history
* Add Ruff linter + Apply Ruff fix

* Move up linting

* Move up linting

* Move up linting

* docs

* Autoformatting, fix D rules

* isort skip

* Fix N818, E721

* drop comment
  • Loading branch information
SharonHart authored May 12, 2024
1 parent 51dc5c6 commit 2d92539
Show file tree
Hide file tree
Showing 36 changed files with 110 additions and 108 deletions.
1 change: 1 addition & 0 deletions presidio-analyzer/presidio_analyzer/nlp_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .spacy_nlp_engine import SpacyNlpEngine
from .stanza_nlp_engine import StanzaNlpEngine
from .transformers_nlp_engine import TransformersNlpEngine

from .nlp_engine_provider import NlpEngineProvider # isort:skip

__all__ = [
Expand Down
4 changes: 2 additions & 2 deletions presidio-anonymizer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from flask import Flask, Response, jsonify, request
from presidio_anonymizer import AnonymizerEngine, DeanonymizeEngine
from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError
from presidio_anonymizer.services.app_entities_convertor import AppEntitiesConvertor
from werkzeug.exceptions import BadRequest, HTTPException

Expand Down Expand Up @@ -96,7 +96,7 @@ def deanonymizers():
"""Return a list of supported deanonymizers."""
return jsonify(self.deanonymize.get_deanonymizers())

@self.app.errorhandler(InvalidParamException)
@self.app.errorhandler(InvalidParamError)
def invalid_param(err):
self.logger.warning(
f"Request failed with parameter validation error: {err.err_msg}"
Expand Down
4 changes: 2 additions & 2 deletions presidio-anonymizer/presidio_anonymizer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
ConflictResolutionStrategy,
DictRecognizerResult,
EngineResult,
InvalidParamException,
InvalidParamError,
OperatorConfig,
OperatorResult,
PIIEntity,
Expand All @@ -25,7 +25,7 @@
"AnonymizerEngine",
"DeanonymizeEngine",
"BatchAnonymizerEngine",
"InvalidParamException",
"InvalidParamError",
"ConflictResolutionStrategy",
"PIIEntity",
"OperatorConfig",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError


class TextReplaceBuilder:
Expand Down Expand Up @@ -55,4 +55,4 @@ def __validate_position_in_text(self, start: int, end: int):
f"Invalid analyzer result, start: {start} and end: "
f"{end}, while text length is only {self.text_len}."
)
raise InvalidParamException(err_msg)
raise InvalidParamError(err_msg)
5 changes: 3 additions & 2 deletions presidio-anonymizer/presidio_anonymizer/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""Handles all the entities objects (structs) of the anonymizer."""

from .invalid_exception import InvalidParamException # isort:skip
from .invalid_exception import InvalidParamError # isort:skip
from .conflict_resolution_strategy import ConflictResolutionStrategy
from .engine.operator_config import OperatorConfig
from .engine.pii_entity import PIIEntity
from .engine.recognizer_result import RecognizerResult
from .engine.result.engine_result import EngineResult
from .engine.result.operator_result import OperatorResult

from .engine.dict_recognizer_result import DictRecognizerResult # isort: skip

__all__ = [
"InvalidParamException",
"InvalidParamError",
"ConflictResolutionStrategy",
"PIIEntity",
"OperatorConfig",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from abc import ABC

from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError
from presidio_anonymizer.services.validators import (
validate_parameter_exists,
validate_parameter_not_empty,
Expand Down Expand Up @@ -47,11 +47,11 @@ def __validate_fields(self):
validate_type(self.end, "end", int)
validate_parameter_not_empty(self.entity_type, "result", "entity_type")
if self.start < 0 or self.end < 0:
raise InvalidParamException(
raise InvalidParamError(
"Invalid input, result start and end must be positive"
)
if self.start > self.end:
raise InvalidParamException(
raise InvalidParamError(
f"Invalid input, start index '{self.start}' "
f"must be smaller than end index '{self.end}'"
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Exception to indicate the request we received is invalid."""


class InvalidParamException(Exception):
class InvalidParamError(Exception):
"""Throw exception with error when user input is not valid.
param msg: Message to be added to the exception
Expand Down
2 changes: 2 additions & 0 deletions presidio-anonymizer/presidio_anonymizer/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from .custom import Custom
from .deanonymize_keep import DeanonymizeKeep
from .encrypt import Encrypt

from .decrypt import Decrypt # isort:skip
from .hash import Hash
from .keep import Keep
from .mask import Mask
from .redact import Redact
from .replace import Replace

from .operators_factory import OperatorsFactory # isort:skip

__all__ = [
Expand Down
8 changes: 4 additions & 4 deletions presidio-anonymizer/presidio_anonymizer/operators/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Dict

from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError
from presidio_anonymizer.operators import Operator, OperatorType


Expand All @@ -24,11 +24,11 @@ def validate(self, params: Dict) -> None:
"""Validate the provided function is returning a string."""
new_val = params.get(self.LAMBDA)
if callable(new_val):
if not type(new_val("PII")) == str:
raise InvalidParamException("Function return type must be a str")
if not isinstance(new_val("PII"), str):
raise InvalidParamError("Function return type must be a str")

else:
raise InvalidParamException("New value must be a callable function")
raise InvalidParamError("New value must be a callable function")

def operator_name(self) -> str:
"""Return operator name."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def operate(self, text: str = None, params: Dict = None) -> str:
:return: The encrypted text
"""
key = params.get(self.KEY)
if type(key) is str:
if isinstance(key, str):
key = key.encode("utf8")
decrypted_text = AESCipher.decrypt(key=key, text=text)
return decrypted_text
Expand Down
10 changes: 5 additions & 5 deletions presidio-anonymizer/presidio_anonymizer/operators/encrypt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError
from presidio_anonymizer.operators import Operator, OperatorType
from presidio_anonymizer.operators.aes_cipher import AESCipher
from presidio_anonymizer.services.validators import validate_parameter
Expand All @@ -21,7 +21,7 @@ def operate(self, text: str = None, params: Dict = None) -> str:
:return: The encrypted text
"""
key = params.get(self.KEY)
if type(key) is str:
if isinstance(key, str):
key = key.encode("utf8")
encrypted_text = AESCipher.encrypt(key, text)
return encrypted_text
Expand All @@ -36,16 +36,16 @@ def validate(self, params: Dict = None) -> None:
:raises InvalidParamException in case on an invalid parameter.
"""
key = params.get(self.KEY)
if type(key) is str:
if isinstance(key, str):
validate_parameter(key, self.KEY, str)
if not AESCipher.is_valid_key_size(key.encode("utf8")):
raise InvalidParamException(
raise InvalidParamError(
f"Invalid input, {self.KEY} must be of length 128, 192 or 256 bits"
)
else:
validate_parameter(key, self.KEY, bytes)
if not AESCipher.is_valid_key_size(key):
raise InvalidParamException(
raise InvalidParamError(
f"Invalid input, {self.KEY} must be of length 128, 192 or 256 bits"
)

Expand Down
4 changes: 2 additions & 2 deletions presidio-anonymizer/presidio_anonymizer/operators/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Dict

from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError
from presidio_anonymizer.operators import Operator, OperatorType
from presidio_anonymizer.services.validators import validate_parameter

Expand Down Expand Up @@ -46,7 +46,7 @@ def validate(self, params: Dict = None) -> None:
masking_char = params.get(self.MASKING_CHAR)
validate_parameter(masking_char, self.MASKING_CHAR, str)
if len(masking_char) > 1:
raise InvalidParamException(
raise InvalidParamError(
f"Invalid input, {self.MASKING_CHAR} must be a character"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from typing import Dict, Type

from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError
from presidio_anonymizer.operators import (
Custom,
DeanonymizeKeep,
Expand Down Expand Up @@ -89,7 +89,7 @@ def remove_anonymize_operator(self, operator: Type[Operator]):
logger.error(
f"Operator {operator().operator_name()} not found in anonymizers list"
)
raise InvalidParamException(
raise InvalidParamError(
f"Operator {operator().operator_name()} not found in anonymizers list"
)
self._anonymizers.pop(operator().operator_name(), None)
Expand All @@ -103,7 +103,7 @@ def remove_deanonymize_operator(self, operator: Type[Operator]):
logger.error(
f"Operator {operator().operator_name()} not found in deanonymizers list"
)
raise InvalidParamException(
raise InvalidParamError(
f"Operator {operator().operator_name()} not found in deanonymizers list"
)
self._deanonymizers.pop(operator().operator_name(), None)
Expand All @@ -122,12 +122,12 @@ def create_operator_class(
operators_by_type = self.__get_operators_classes().get(operator_type)
if not operators_by_type:
logger.error(f"No such operator type {operator_type}")
raise InvalidParamException(f"Invalid operator type '{operator_type}'.")
raise InvalidParamError(f"Invalid operator type '{operator_type}'.")

operator = operators_by_type.get(operator_name)
if not operator:
logger.error(f"No such operator {operator_name}")
raise InvalidParamException(f"Invalid operator class '{operator_name}'.")
raise InvalidParamError(f"Invalid operator class '{operator_name}'.")

return operator()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, List

from presidio_anonymizer.entities import (
InvalidParamException,
InvalidParamError,
OperatorConfig,
OperatorResult,
RecognizerResult,
Expand All @@ -19,7 +19,7 @@ def analyzer_results_from_json(data: List[Dict]) -> List["RecognizerResult"]:
:param data: contains the anonymizers and analyzer_results_json
"""
if data is None:
raise InvalidParamException(
raise InvalidParamError(
"Invalid input, " "request must contain analyzer results"
)
return [RecognizerResult.from_json(analyzer_result) for analyzer_result in data]
Expand Down
12 changes: 6 additions & 6 deletions presidio-anonymizer/presidio_anonymizer/services/validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Anomnymizers validations utility methods."""

from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError


def validate_parameter_in_range(
Expand All @@ -15,7 +15,7 @@ def validate_parameter_in_range(
"""
validate_parameter(parameter_value, parameter_name, object)
if parameter_value not in values_range:
raise InvalidParamException(
raise InvalidParamError(
f"Parameter {parameter_name} value {parameter_value} is not in "
f"range of values {values_range}"
)
Expand All @@ -26,7 +26,7 @@ def validate_parameter_not_empty(
) -> None:
"""Validate parameter exists and not only empty."""
if not parameter_value:
raise InvalidParamException(
raise InvalidParamError(
f"Invalid input, {entity} must contain {parameter_name}"
)

Expand All @@ -36,7 +36,7 @@ def validate_parameter_exists(
) -> None:
"""Validate parameter is not empty."""
if parameter_value is None:
raise InvalidParamException(
raise InvalidParamError(
f"Invalid input, {entity} must contain {parameter_name}"
)

Expand All @@ -51,7 +51,7 @@ def validate_parameter(
InvalidParamException with the parameter_name as content.
"""
if parameter_value is None:
raise InvalidParamException(f"Expected parameter {parameter_name}")
raise InvalidParamError(f"Expected parameter {parameter_name}")
validate_type(parameter_value, parameter_name, parameter_type)


Expand All @@ -69,7 +69,7 @@ def validate_type(parameter_value, parameter_name, parameter_type):
expected_type=parameter_type,
actual_type=type(parameter_value),
)
raise InvalidParamException(message)
raise InvalidParamError(message)


def _get_bad_typed_parameter_error_message(parameter_name, expected_type, actual_type):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.entities import (
InvalidParamException,
InvalidParamError,
RecognizerResult,
OperatorConfig,
)
Expand Down Expand Up @@ -40,7 +40,7 @@ def test_given_operator_decrypt_then_we_fail():
]
engine = AnonymizerEngine()
with pytest.raises(
InvalidParamException,
InvalidParamError,
match="Invalid operator class 'decrypt'.",
):
engine.anonymize(text, analyzer_results, anonymizers_config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from presidio_anonymizer import AnonymizerEngine
from presidio_anonymizer.deanonymize_engine import DeanonymizeEngine
from presidio_anonymizer.entities import (
InvalidParamException,
InvalidParamError,
RecognizerResult,
OperatorResult,
OperatorConfig,
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_given_short_key_then_we_fail():
]
engine = DeanonymizeEngine()
expected_result = "Invalid input, key must be of length 128, 192 or 256 bits"
with pytest.raises(InvalidParamException, match=expected_result):
with pytest.raises(InvalidParamError, match=expected_result):
engine.deanonymize(
text,
encryption_results,
Expand Down
6 changes: 3 additions & 3 deletions presidio-anonymizer/tests/operators/test_custom.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest

from presidio_anonymizer.operators import Custom
from presidio_anonymizer.entities import InvalidParamException
from presidio_anonymizer.entities import InvalidParamError


def test_given_non_callable_for_custom_then_ipe_raised():
with pytest.raises(
InvalidParamException,
InvalidParamError,
match="New value must be a callable function",
):
Custom().validate({"lambda": "bla"})
Expand All @@ -19,7 +19,7 @@ def test_given_lambda_for_custom_we_get_the_result_back():

def test_given_non_str_lambda_than_ipe_raised():
with pytest.raises(
InvalidParamException,
InvalidParamError,
match="Function return type must be a str",
):
Custom().validate({"lambda": lambda x: len(x)})
Expand Down
Loading

0 comments on commit 2d92539

Please sign in to comment.