From 2d92539fcab6c640aa8e2960b7a29bb33a0d48f9 Mon Sep 17 00:00:00 2001 From: Sharon Hart Date: Sun, 12 May 2024 15:05:41 +0300 Subject: [PATCH] Fix N818, E721 (#1382) * 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 --- .../presidio_analyzer/nlp_engine/__init__.py | 1 + presidio-anonymizer/app.py | 4 ++-- .../presidio_anonymizer/__init__.py | 4 ++-- .../core/text_replace_builder.py | 4 ++-- .../presidio_anonymizer/entities/__init__.py | 5 +++-- .../entities/engine/pii_entity.py | 6 +++--- .../entities/invalid_exception.py | 2 +- .../presidio_anonymizer/operators/__init__.py | 2 ++ .../presidio_anonymizer/operators/custom.py | 8 ++++---- .../presidio_anonymizer/operators/decrypt.py | 2 +- .../presidio_anonymizer/operators/encrypt.py | 10 +++++----- .../presidio_anonymizer/operators/mask.py | 4 ++-- .../operators/operators_factory.py | 10 +++++----- .../services/app_entities_convertor.py | 4 ++-- .../services/validators.py | 12 +++++------ .../integration/test_anonymize_engine.py | 4 ++-- .../integration/test_deanonymize_engine.py | 4 ++-- .../tests/operators/test_custom.py | 6 +++--- .../tests/operators/test_decrypt.py | 4 ++-- .../tests/operators/test_encrypt.py | 4 ++-- .../tests/operators/test_hash.py | 6 +++--- .../tests/operators/test_mask.py | 16 +++++++-------- .../tests/operators/test_operators_factory.py | 12 +++++------ .../tests/services/test_validators.py | 20 +++++++++---------- .../tests/test_anonymizer_engine.py | 6 +++--- .../tests/test_app_entities_convertor.py | 6 +++--- .../tests/test_operator_config.py | 4 ++-- .../tests/test_recognizer_result.py | 10 +++++----- .../tests/test_text_replace_builder.py | 4 ++-- presidio-image-redactor/app.py | 6 +++--- .../dicom_image_redactor_engine.py | 2 +- .../entities/__init__.py | 4 ++-- .../entities/api_request_convertor.py | 8 ++++---- .../entities/invalid_exception.py | 2 +- .../tests/test_api_request_convertor.py | 8 ++++---- pyproject.toml | 4 +--- 36 files changed, 110 insertions(+), 108 deletions(-) diff --git a/presidio-analyzer/presidio_analyzer/nlp_engine/__init__.py b/presidio-analyzer/presidio_analyzer/nlp_engine/__init__.py index a3bd36167..d991e56b8 100644 --- a/presidio-analyzer/presidio_analyzer/nlp_engine/__init__.py +++ b/presidio-analyzer/presidio_analyzer/nlp_engine/__init__.py @@ -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__ = [ diff --git a/presidio-anonymizer/app.py b/presidio-anonymizer/app.py index f0240fd99..12ce67c02 100644 --- a/presidio-anonymizer/app.py +++ b/presidio-anonymizer/app.py @@ -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 @@ -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}" diff --git a/presidio-anonymizer/presidio_anonymizer/__init__.py b/presidio-anonymizer/presidio_anonymizer/__init__.py index 119a33961..11b09fb7d 100644 --- a/presidio-anonymizer/presidio_anonymizer/__init__.py +++ b/presidio-anonymizer/presidio_anonymizer/__init__.py @@ -9,7 +9,7 @@ ConflictResolutionStrategy, DictRecognizerResult, EngineResult, - InvalidParamException, + InvalidParamError, OperatorConfig, OperatorResult, PIIEntity, @@ -25,7 +25,7 @@ "AnonymizerEngine", "DeanonymizeEngine", "BatchAnonymizerEngine", - "InvalidParamException", + "InvalidParamError", "ConflictResolutionStrategy", "PIIEntity", "OperatorConfig", diff --git a/presidio-anonymizer/presidio_anonymizer/core/text_replace_builder.py b/presidio-anonymizer/presidio_anonymizer/core/text_replace_builder.py index cc99f4d9e..739a220a8 100644 --- a/presidio-anonymizer/presidio_anonymizer/core/text_replace_builder.py +++ b/presidio-anonymizer/presidio_anonymizer/core/text_replace_builder.py @@ -2,7 +2,7 @@ import logging -from presidio_anonymizer.entities import InvalidParamException +from presidio_anonymizer.entities import InvalidParamError class TextReplaceBuilder: @@ -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) diff --git a/presidio-anonymizer/presidio_anonymizer/entities/__init__.py b/presidio-anonymizer/presidio_anonymizer/entities/__init__.py index da0f33fd3..6538c6e7b 100644 --- a/presidio-anonymizer/presidio_anonymizer/entities/__init__.py +++ b/presidio-anonymizer/presidio_anonymizer/entities/__init__.py @@ -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", diff --git a/presidio-anonymizer/presidio_anonymizer/entities/engine/pii_entity.py b/presidio-anonymizer/presidio_anonymizer/entities/engine/pii_entity.py index dab98185c..876cc20b1 100644 --- a/presidio-anonymizer/presidio_anonymizer/entities/engine/pii_entity.py +++ b/presidio-anonymizer/presidio_anonymizer/entities/engine/pii_entity.py @@ -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, @@ -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}'" ) diff --git a/presidio-anonymizer/presidio_anonymizer/entities/invalid_exception.py b/presidio-anonymizer/presidio_anonymizer/entities/invalid_exception.py index a3c3dcfd0..8c39b0fab 100644 --- a/presidio-anonymizer/presidio_anonymizer/entities/invalid_exception.py +++ b/presidio-anonymizer/presidio_anonymizer/entities/invalid_exception.py @@ -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 diff --git a/presidio-anonymizer/presidio_anonymizer/operators/__init__.py b/presidio-anonymizer/presidio_anonymizer/operators/__init__.py index b5be3ed6d..d61b05171 100644 --- a/presidio-anonymizer/presidio_anonymizer/operators/__init__.py +++ b/presidio-anonymizer/presidio_anonymizer/operators/__init__.py @@ -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__ = [ diff --git a/presidio-anonymizer/presidio_anonymizer/operators/custom.py b/presidio-anonymizer/presidio_anonymizer/operators/custom.py index dffdd3d0e..a7c3e3844 100644 --- a/presidio-anonymizer/presidio_anonymizer/operators/custom.py +++ b/presidio-anonymizer/presidio_anonymizer/operators/custom.py @@ -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 @@ -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.""" diff --git a/presidio-anonymizer/presidio_anonymizer/operators/decrypt.py b/presidio-anonymizer/presidio_anonymizer/operators/decrypt.py index e18e7c06e..5dbd6f912 100644 --- a/presidio-anonymizer/presidio_anonymizer/operators/decrypt.py +++ b/presidio-anonymizer/presidio_anonymizer/operators/decrypt.py @@ -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 diff --git a/presidio-anonymizer/presidio_anonymizer/operators/encrypt.py b/presidio-anonymizer/presidio_anonymizer/operators/encrypt.py index 7a7f80955..9cd11d4ae 100644 --- a/presidio-anonymizer/presidio_anonymizer/operators/encrypt.py +++ b/presidio-anonymizer/presidio_anonymizer/operators/encrypt.py @@ -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 @@ -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 @@ -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" ) diff --git a/presidio-anonymizer/presidio_anonymizer/operators/mask.py b/presidio-anonymizer/presidio_anonymizer/operators/mask.py index 6fb0024f9..70773d2f7 100644 --- a/presidio-anonymizer/presidio_anonymizer/operators/mask.py +++ b/presidio-anonymizer/presidio_anonymizer/operators/mask.py @@ -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 @@ -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" ) diff --git a/presidio-anonymizer/presidio_anonymizer/operators/operators_factory.py b/presidio-anonymizer/presidio_anonymizer/operators/operators_factory.py index c3b670c22..f621248cc 100644 --- a/presidio-anonymizer/presidio_anonymizer/operators/operators_factory.py +++ b/presidio-anonymizer/presidio_anonymizer/operators/operators_factory.py @@ -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, @@ -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) @@ -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) @@ -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() diff --git a/presidio-anonymizer/presidio_anonymizer/services/app_entities_convertor.py b/presidio-anonymizer/presidio_anonymizer/services/app_entities_convertor.py index 6dd2ee327..a2a7e3cea 100644 --- a/presidio-anonymizer/presidio_anonymizer/services/app_entities_convertor.py +++ b/presidio-anonymizer/presidio_anonymizer/services/app_entities_convertor.py @@ -1,7 +1,7 @@ from typing import Dict, List from presidio_anonymizer.entities import ( - InvalidParamException, + InvalidParamError, OperatorConfig, OperatorResult, RecognizerResult, @@ -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] diff --git a/presidio-anonymizer/presidio_anonymizer/services/validators.py b/presidio-anonymizer/presidio_anonymizer/services/validators.py index 9bfb4b03c..57c050f2e 100644 --- a/presidio-anonymizer/presidio_anonymizer/services/validators.py +++ b/presidio-anonymizer/presidio_anonymizer/services/validators.py @@ -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( @@ -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}" ) @@ -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}" ) @@ -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}" ) @@ -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) @@ -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): diff --git a/presidio-anonymizer/tests/integration/test_anonymize_engine.py b/presidio-anonymizer/tests/integration/test_anonymize_engine.py index 710359c30..bc68217ca 100644 --- a/presidio-anonymizer/tests/integration/test_anonymize_engine.py +++ b/presidio-anonymizer/tests/integration/test_anonymize_engine.py @@ -4,7 +4,7 @@ from presidio_anonymizer import AnonymizerEngine from presidio_anonymizer.entities import ( - InvalidParamException, + InvalidParamError, RecognizerResult, OperatorConfig, ) @@ -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) diff --git a/presidio-anonymizer/tests/integration/test_deanonymize_engine.py b/presidio-anonymizer/tests/integration/test_deanonymize_engine.py index dffdee692..92d78f779 100644 --- a/presidio-anonymizer/tests/integration/test_deanonymize_engine.py +++ b/presidio-anonymizer/tests/integration/test_deanonymize_engine.py @@ -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, @@ -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, diff --git a/presidio-anonymizer/tests/operators/test_custom.py b/presidio-anonymizer/tests/operators/test_custom.py index 0d372dc80..cce0ee197 100644 --- a/presidio-anonymizer/tests/operators/test_custom.py +++ b/presidio-anonymizer/tests/operators/test_custom.py @@ -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"}) @@ -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)}) diff --git a/presidio-anonymizer/tests/operators/test_decrypt.py b/presidio-anonymizer/tests/operators/test_decrypt.py index 46464c22c..0b0213ec2 100644 --- a/presidio-anonymizer/tests/operators/test_decrypt.py +++ b/presidio-anonymizer/tests/operators/test_decrypt.py @@ -2,7 +2,7 @@ import pytest -from presidio_anonymizer.entities import InvalidParamException +from presidio_anonymizer.entities import InvalidParamError from presidio_anonymizer.operators import Decrypt, AESCipher @@ -41,7 +41,7 @@ def test_given_verifying_an_valid_length_bytes_key_no_exceptions_raised(): def test_given_verifying_an_invalid_length_key_then_ipe_raised(): with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid input, key must be of length 128, 192 or 256 bits", ): Decrypt().validate(params={"key": "key"}) diff --git a/presidio-anonymizer/tests/operators/test_encrypt.py b/presidio-anonymizer/tests/operators/test_encrypt.py index 5c6c8fb65..04704b511 100644 --- a/presidio-anonymizer/tests/operators/test_encrypt.py +++ b/presidio-anonymizer/tests/operators/test_encrypt.py @@ -3,7 +3,7 @@ import pytest from presidio_anonymizer.operators import Encrypt, AESCipher -from presidio_anonymizer.entities import InvalidParamException +from presidio_anonymizer.entities import InvalidParamError @mock.patch.object(AESCipher, "encrypt") @@ -41,7 +41,7 @@ def test_given_verifying_an_valid_length_bytes_key_no_exceptions_raised(): def test_given_verifying_an_invalid_length_key_then_ipe_raised(): with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid input, key must be of length 128, 192 or 256 bits", ): Encrypt().validate(params={"key": "key"}) diff --git a/presidio-anonymizer/tests/operators/test_hash.py b/presidio-anonymizer/tests/operators/test_hash.py index fbcace362..1595c4351 100644 --- a/presidio-anonymizer/tests/operators/test_hash.py +++ b/presidio-anonymizer/tests/operators/test_hash.py @@ -1,7 +1,7 @@ import pytest from presidio_anonymizer.operators import Hash -from presidio_anonymizer.entities import InvalidParamException +from presidio_anonymizer.entities import InvalidParamError @pytest.mark.parametrize( @@ -97,7 +97,7 @@ def test_when_hash_type_not_in_range_then_ipe_raised(): params["hash_type"] = "not_a_hash" with pytest.raises( - InvalidParamException, + InvalidParamError, match="Parameter hash_type value not_a_hash is not in range of values" " \\['sha256', 'sha512', 'md5'\\]", ): @@ -116,7 +116,7 @@ def test_when_hash_type_is_empty_string_then_ipe_raised(): params["hash_type"] = "" with pytest.raises( - InvalidParamException, + InvalidParamError, match="Parameter hash_type value is not in range of values" " \\['sha256', 'sha512', 'md5'\\]", ): diff --git a/presidio-anonymizer/tests/operators/test_mask.py b/presidio-anonymizer/tests/operators/test_mask.py index dea8034ea..d2d83c468 100644 --- a/presidio-anonymizer/tests/operators/test_mask.py +++ b/presidio-anonymizer/tests/operators/test_mask.py @@ -1,7 +1,7 @@ import pytest from presidio_anonymizer.operators import Mask -from presidio_anonymizer.entities import InvalidParamException +from presidio_anonymizer.entities import InvalidParamError @pytest.mark.parametrize( @@ -47,7 +47,7 @@ def test_when_masking_char_is_missing_then_ipe_raised(): params = _get_default_mask_parameters() params.pop("masking_char") - with pytest.raises(InvalidParamException, match="Expected parameter masking_char"): + with pytest.raises(InvalidParamError, match="Expected parameter masking_char"): Mask().validate(params) @@ -56,7 +56,7 @@ def test_when_masking_char_is_bad_typed_then_ipe_raised(): params["masking_char"] = 1 with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid parameter value for masking_char. " "Expecting 'string', but got 'number'.", ): @@ -68,7 +68,7 @@ def test_when_masking_char_length_is_greater_than_one_then_ipe_raised(): params["masking_char"] = "string_not_character" with pytest.raises( - InvalidParamException, match="Invalid input, masking_char must be a character" + InvalidParamError, match="Invalid input, masking_char must be a character" ): Mask().validate(params) @@ -77,7 +77,7 @@ def test_when_chars_to_mask_is_missing_then_ipe_raised(): params = _get_default_mask_parameters() params.pop("chars_to_mask") - with pytest.raises(InvalidParamException, match="Expected parameter chars_to_mask"): + with pytest.raises(InvalidParamError, match="Expected parameter chars_to_mask"): Mask().validate(params) @@ -86,7 +86,7 @@ def test_when_chars_to_mask_bad_typed_then_ipe_raised(): params["chars_to_mask"] = "not_an_integer" with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid parameter value for chars_to_mask. " "Expecting 'number', but got 'string'.", ): @@ -97,7 +97,7 @@ def test_when_from_end_is_missing_then_ipe_raised(): params = _get_default_mask_parameters() params.pop("from_end") - with pytest.raises(InvalidParamException, match="Expected parameter from_end"): + with pytest.raises(InvalidParamError, match="Expected parameter from_end"): Mask().validate(params) @@ -106,7 +106,7 @@ def test_when_from_end_is_bad_typed_then_ipe_raised(): params["from_end"] = "not_a_boolean" with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid parameter value for from_end. " "Expecting 'boolean', but got 'string'.", ): diff --git a/presidio-anonymizer/tests/operators/test_operators_factory.py b/presidio-anonymizer/tests/operators/test_operators_factory.py index e8880119a..91100c346 100644 --- a/presidio-anonymizer/tests/operators/test_operators_factory.py +++ b/presidio-anonymizer/tests/operators/test_operators_factory.py @@ -1,6 +1,6 @@ import pytest -from presidio_anonymizer.entities import InvalidParamException +from presidio_anonymizer.entities import InvalidParamError from presidio_anonymizer.operators import OperatorsFactory, OperatorType @@ -51,20 +51,20 @@ def test_given_decrypt_operator_class_then_we_get_the_correct_class(): def test_given_wrong_name_class_then_we_fail(): with pytest.raises( - InvalidParamException, match="Invalid operator class 'encrypt'." + InvalidParamError, match="Invalid operator class 'encrypt'." ): OperatorsFactory().create_operator_class("encrypt", OperatorType.Deanonymize) def test_given_wrong_name_for_anonymizer_class_then_we_fail(): with pytest.raises( - InvalidParamException, match="Invalid operator class 'decrypt'." + InvalidParamError, match="Invalid operator class 'decrypt'." ): OperatorsFactory().create_operator_class("decrypt", OperatorType.Anonymize) def test_given_wrong_operator_then_we_fail(): - with pytest.raises(InvalidParamException, match="Invalid operator type '3'."): + with pytest.raises(InvalidParamError, match="Invalid operator type '3'."): OperatorsFactory().create_operator_class("bla", 3) @@ -101,7 +101,7 @@ def test_remove_anonymizer_removes_operator(mock_anonymizer_cls): def test_remove_missing_anonymizer_raises_exception(mock_anonymizer_cls): factory = OperatorsFactory() with pytest.raises( - InvalidParamException, + InvalidParamError, match="Operator MockAnonymizer not found in anonymizers list", ): factory.remove_anonymize_operator(mock_anonymizer_cls) @@ -119,7 +119,7 @@ def test_remove_deanonymizer_removes_operator(mock_deanonymizer_cls): def test_remove_missing_deanonymizer_raises_exception(mock_deanonymizer_cls): factory = OperatorsFactory() with pytest.raises( - InvalidParamException, + InvalidParamError, match="Operator MockDeanonymizer not found in deanonymizers list", ): factory.remove_deanonymize_operator(mock_deanonymizer_cls) diff --git a/presidio-anonymizer/tests/services/test_validators.py b/presidio-anonymizer/tests/services/test_validators.py index 81a6287c3..febbba68d 100644 --- a/presidio-anonymizer/tests/services/test_validators.py +++ b/presidio-anonymizer/tests/services/test_validators.py @@ -1,6 +1,6 @@ import pytest -from presidio_anonymizer.entities import InvalidParamException +from presidio_anonymizer.entities import InvalidParamError from presidio_anonymizer.services.validators import ( validate_parameter, validate_parameter_exists, @@ -20,7 +20,7 @@ def test_given_empty_string_then_no_exception_raised(): def test_given_no_existing_parameter_then_exception_raised(): with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid input, entity must contain name", ): validate_parameter_exists(None, "entity", "name") @@ -32,7 +32,7 @@ def test_given_existing_parameter_then_no_exception_raised(): def test_given_empty_parameter_then_exception_raised(): with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid input, entity must contain name", ): validate_parameter_not_empty("", "entity", "name") @@ -40,7 +40,7 @@ def test_given_empty_parameter_then_exception_raised(): def test_given_parameter_does_not_exist_then_exception_raised(): with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid input, entity must contain name", ): validate_parameter_not_empty(None, "entity", "name") @@ -48,7 +48,7 @@ def test_given_parameter_does_not_exist_then_exception_raised(): def test_given_parameter_is_0_then_exception_raised(): with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid input, entity must contain name", ): validate_parameter_not_empty(0, "entity", "name") @@ -56,7 +56,7 @@ def test_given_parameter_is_0_then_exception_raised(): def test_given_parameter_not_in_range_then_ipe_raised(): with pytest.raises( - InvalidParamException, + InvalidParamError, match="Parameter name value 1 is not in range of values \\['0', '2'\\]", ): validate_parameter_in_range( @@ -77,7 +77,7 @@ def test_given_parameter_in_range_then_we_pass(): def test_given_parameter_is_none_typed_then_ipe_raised(): - with pytest.raises(InvalidParamException, match="Expected parameter name"): + with pytest.raises(InvalidParamError, match="Expected parameter name"): validate_parameter( parameter_value=None, parameter_name="name", parameter_type=int ) @@ -86,7 +86,7 @@ def test_given_parameter_is_none_typed_then_ipe_raised(): def test_given_parameter_is_bad_typed_then_ipe_raised(): err = "Invalid parameter value for name. Expecting 'number', but got 'string'." with pytest.raises( - InvalidParamException, + InvalidParamError, match=err, ): validate_parameter( @@ -96,7 +96,7 @@ def test_given_parameter_is_bad_typed_then_ipe_raised(): def test_given_actual_parameter_is_non_json_typed_then_ipe_raised_with_general_error(): with pytest.raises( - InvalidParamException, match="Invalid parameter value for 'name'." + InvalidParamError, match="Invalid parameter value for 'name'." ): validate_parameter( parameter_value="1", parameter_name="name", parameter_type=tuple @@ -105,7 +105,7 @@ def test_given_actual_parameter_is_non_json_typed_then_ipe_raised_with_general_e def test_given_wrong_type_then_we_fail(): err_str = "Invalid parameter value for name. Expecting 'string', but got 'number'." - with pytest.raises(InvalidParamException, match=err_str): + with pytest.raises(InvalidParamError, match=err_str): validate_type(parameter_value=1, parameter_name="name", parameter_type=str) diff --git a/presidio-anonymizer/tests/test_anonymizer_engine.py b/presidio-anonymizer/tests/test_anonymizer_engine.py index 6a0782519..6e8c6d33a 100644 --- a/presidio-anonymizer/tests/test_anonymizer_engine.py +++ b/presidio-anonymizer/tests/test_anonymizer_engine.py @@ -4,7 +4,7 @@ from presidio_anonymizer import AnonymizerEngine from presidio_anonymizer.entities import ( - InvalidParamException, + InvalidParamError, RecognizerResult, OperatorConfig, PIIEntity, @@ -106,7 +106,7 @@ def test_given_analyzer_result_with_an_incorrect_text_positions_then_we_fail( f"Invalid analyzer result, start: {start} and end: " f"{end}, while text length is only 11." ) - with pytest.raises(InvalidParamException, match=err_msg): + with pytest.raises(InvalidParamError, match=err_msg): engine.anonymize(original_text, [analyzer_result], {}) @@ -119,7 +119,7 @@ def test_given_analyzer_result_with_an_incorrect_text_positions_then_we_fail( # fmt: on ) def test_given_invalid_json_for_anonymizers_then_we_fail(anonymizers, result_text): - with pytest.raises(InvalidParamException, match=result_text): + with pytest.raises(InvalidParamError, match=result_text): AnonymizerEngine().anonymize( "this is my text", [RecognizerResult("number", 0, 4, 0)], anonymizers ) diff --git a/presidio-anonymizer/tests/test_app_entities_convertor.py b/presidio-anonymizer/tests/test_app_entities_convertor.py index 62597ecbe..5f1e2b41e 100644 --- a/presidio-anonymizer/tests/test_app_entities_convertor.py +++ b/presidio-anonymizer/tests/test_app_entities_convertor.py @@ -3,7 +3,7 @@ import pytest from presidio_anonymizer.entities import ( - InvalidParamException, + InvalidParamError, RecognizerResult, OperatorConfig, ) @@ -45,7 +45,7 @@ def test_given_valid_json_then_anonymizers_config_list_created_successfully(): # fmt: on ) def test_given_invalid_json_for_analyzer_result_then_we_fail(request_json, result_text): - with pytest.raises(InvalidParamException) as e: + with pytest.raises(InvalidParamError) as e: AppEntitiesConvertor.analyzer_results_from_json(request_json) assert result_text == e.value.err_msg @@ -147,7 +147,7 @@ def test_given_invalid_json_then_we_fail_to_convert(): ], } with pytest.raises( - InvalidParamException, match="Invalid input, result must contain entity_type" + InvalidParamError, match="Invalid input, result must contain entity_type" ): AppEntitiesConvertor.deanonymize_entities_from_json(data) diff --git a/presidio-anonymizer/tests/test_operator_config.py b/presidio-anonymizer/tests/test_operator_config.py index 377d681ca..6cd49770b 100644 --- a/presidio-anonymizer/tests/test_operator_config.py +++ b/presidio-anonymizer/tests/test_operator_config.py @@ -1,6 +1,6 @@ import pytest -from presidio_anonymizer.entities import InvalidParamException, OperatorConfig +from presidio_anonymizer.entities import InvalidParamError, OperatorConfig def test_given_valid_json_then_we_parse_it_to_operator_config(): @@ -17,7 +17,7 @@ def test_given_valid_json_then_we_parse_it_to_operator_config(): def test_given_invalid_json_then_we_fail_to_parse_it_to_operator_config(): expected_error = "Invalid input, operator config must contain operator_name" - with pytest.raises(InvalidParamException, match=expected_error): + with pytest.raises(InvalidParamError, match=expected_error): OperatorConfig.from_json( {"masking_char": "*", "chars_to_mask": 4, "from_end": True} ) diff --git a/presidio-anonymizer/tests/test_recognizer_result.py b/presidio-anonymizer/tests/test_recognizer_result.py index f29a6aea6..bbdcfbae9 100644 --- a/presidio-anonymizer/tests/test_recognizer_result.py +++ b/presidio-anonymizer/tests/test_recognizer_result.py @@ -1,6 +1,6 @@ import pytest -from presidio_anonymizer.entities import InvalidParamException, RecognizerResult +from presidio_anonymizer.entities import InvalidParamError, RecognizerResult @pytest.mark.parametrize( @@ -78,7 +78,7 @@ def test_given_recognizer_results_with_different_indices_then_indices_are_not_eq # fmt: on ) def test_given_invalid_string_start_instead_of_int_then_we_fail(start, end, err): - with pytest.raises(InvalidParamException, match=err): + with pytest.raises(InvalidParamError, match=err): create_recognizer_result("bla", 0.2, start, end) @@ -206,7 +206,7 @@ def test_given_recognizer_results_with_no_conflicting_indices_then_there_is_no_c def test_given_json_for_creating_recognizer_result_without_text_then_creation_fails( request_json, result_text ): - with pytest.raises(InvalidParamException) as e: + with pytest.raises(InvalidParamError) as e: RecognizerResult.from_json(request_json) assert result_text == e.value.err_msg @@ -255,7 +255,7 @@ def test_given_recognizer_result_then_one_is_not_greater_then_another(start, end def test_given_endpoint_larger_then_start_point_then_we_fail(): - with pytest.raises(InvalidParamException) as e: + with pytest.raises(InvalidParamError) as e: create_recognizer_result("entity", 0, 10, 0) assert ( e.value.err_msg == "Invalid input, start index '10' " @@ -279,7 +279,7 @@ def test_given_endpoint_equal_to_start_point_then_we_succeed(): ) def test_given_negative_start_or_endpoint_then_we_fail(start, end): with pytest.raises( - InvalidParamException, + InvalidParamError, match="Invalid input, result start and end must be positive", ): create_recognizer_result("entity", 0, start, end) diff --git a/presidio-anonymizer/tests/test_text_replace_builder.py b/presidio-anonymizer/tests/test_text_replace_builder.py index 9ab1b2ff6..08ca17de4 100644 --- a/presidio-anonymizer/tests/test_text_replace_builder.py +++ b/presidio-anonymizer/tests/test_text_replace_builder.py @@ -1,5 +1,5 @@ import pytest -from presidio_anonymizer.entities import InvalidParamException +from presidio_anonymizer.entities import InvalidParamError from presidio_anonymizer.core import TextReplaceBuilder @@ -60,5 +60,5 @@ def test_given_text_and_bad_indices_then_we_get_fail(original_text, start, end): f"Invalid analyzer result, start: {start} and end: {end}, " f"while text length is only 11." ) - with pytest.raises(InvalidParamException, match=err_msg): + with pytest.raises(InvalidParamError, match=err_msg): text_replace_builder.get_text_in_position(start, end) diff --git a/presidio-image-redactor/app.py b/presidio-image-redactor/app.py index 2fbb30836..780742eaf 100644 --- a/presidio-image-redactor/app.py +++ b/presidio-image-redactor/app.py @@ -8,7 +8,7 @@ from flask import Flask, Response, jsonify, request from PIL import Image from presidio_image_redactor import ImageRedactorEngine -from presidio_image_redactor.entities import InvalidParamException +from presidio_image_redactor.entities import InvalidParamError from presidio_image_redactor.entities.api_request_convertor import ( color_fill_string_to_value, get_json_data, @@ -66,9 +66,9 @@ def redact(): img_byte_arr = image_to_byte_array(redacted_image, im.format) return Response(img_byte_arr, mimetype="application/octet-stream") else: - raise InvalidParamException("Invalid parameter, please add image data") + raise InvalidParamError("Invalid parameter, please add image data") - @self.app.errorhandler(InvalidParamException) + @self.app.errorhandler(InvalidParamError) def invalid_param(err): self.logger.warning( f"failed to redact image with validation error: {err.err_msg}" diff --git a/presidio-image-redactor/presidio_image_redactor/dicom_image_redactor_engine.py b/presidio-image-redactor/presidio_image_redactor/dicom_image_redactor_engine.py index 4415e94a5..09347d3f4 100644 --- a/presidio-image-redactor/presidio_image_redactor/dicom_image_redactor_engine.py +++ b/presidio-image-redactor/presidio_image_redactor/dicom_image_redactor_engine.py @@ -962,7 +962,7 @@ def _get_analyzer_results( if ad_hoc_recognizers is None: ad_hoc_recognizers = [deny_list_recognizer] - elif type(ad_hoc_recognizers) is list: + elif isinstance(ad_hoc_recognizers, list): ad_hoc_recognizers.append(deny_list_recognizer) # Detect PII diff --git a/presidio-image-redactor/presidio_image_redactor/entities/__init__.py b/presidio-image-redactor/presidio_image_redactor/entities/__init__.py index e6e006e31..b22924b71 100644 --- a/presidio-image-redactor/presidio_image_redactor/entities/__init__.py +++ b/presidio-image-redactor/presidio_image_redactor/entities/__init__.py @@ -1,9 +1,9 @@ """Image Redactor entities.""" from .image_recognizer_result import ImageRecognizerResult -from .invalid_exception import InvalidParamException +from .invalid_exception import InvalidParamError __all__ = [ "ImageRecognizerResult", - "InvalidParamException", + "InvalidParamError", ] diff --git a/presidio-image-redactor/presidio_image_redactor/entities/api_request_convertor.py b/presidio-image-redactor/presidio_image_redactor/entities/api_request_convertor.py index 2e17c062b..5054e2a33 100644 --- a/presidio-image-redactor/presidio_image_redactor/entities/api_request_convertor.py +++ b/presidio-image-redactor/presidio_image_redactor/entities/api_request_convertor.py @@ -5,7 +5,7 @@ from PIL import Image -from presidio_image_redactor.entities import InvalidParamException +from presidio_image_redactor.entities import InvalidParamError logger = logging.getLogger("presidio-image-redactor") @@ -24,7 +24,7 @@ def get_json_data(data: str) -> dict: return json.loads(data.replace("'", '"')) except Exception as e: logger.error(f"failed to parse json from string '{data}' with error {e}") - raise InvalidParamException(f"Invalid json format '{data}'") + raise InvalidParamError(f"Invalid json format '{data}'") def color_fill_string_to_value(json_params: dict) -> Union[int, Tuple[int, int, int]]: @@ -43,11 +43,11 @@ def color_fill_string_to_value(json_params: dict) -> Union[int, Tuple[int, int, if len(filling_str_split) == 1: return int(filling_str_split[0]) if len(filling_str_split) != 3: - raise InvalidParamException(f"Invalid color fill '{filling_str}'") + raise InvalidParamError(f"Invalid color fill '{filling_str}'") return tuple(map(int, filling_str_split)) except Exception as e: logger.error(f"failed to color fill '{filling_str}' with error {e}") - raise InvalidParamException(f"Invalid color fill '{filling_str}'") + raise InvalidParamError(f"Invalid color fill '{filling_str}'") def image_to_byte_array(redacted_image: Image, image_format: str): diff --git a/presidio-image-redactor/presidio_image_redactor/entities/invalid_exception.py b/presidio-image-redactor/presidio_image_redactor/entities/invalid_exception.py index 9c99aee07..f9ab630aa 100644 --- a/presidio-image-redactor/presidio_image_redactor/entities/invalid_exception.py +++ b/presidio-image-redactor/presidio_image_redactor/entities/invalid_exception.py @@ -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.""" def __init__(self, msg: str): diff --git a/presidio-image-redactor/tests/test_api_request_convertor.py b/presidio-image-redactor/tests/test_api_request_convertor.py index b9daa1991..2a5edbce3 100644 --- a/presidio-image-redactor/tests/test_api_request_convertor.py +++ b/presidio-image-redactor/tests/test_api_request_convertor.py @@ -1,6 +1,6 @@ import pytest -from presidio_image_redactor.entities import InvalidParamException +from presidio_image_redactor.entities import InvalidParamError from presidio_image_redactor.entities.api_request_convertor import ( get_json_data, color_fill_string_to_value, @@ -26,7 +26,7 @@ def test_given_json_string_then_we_get_json_back(str_json): def test_given_invalid_json_string_then_we_get_an_invalid_param_exception(): - with pytest.raises(InvalidParamException, match="Invalid json format 'not_json'"): + with pytest.raises(InvalidParamError, match="Invalid json format 'not_json'"): get_json_data("not_json") @@ -51,10 +51,10 @@ def test_given_json_params_then_we_extract_properly_color_fill(json_params, expe # fmt: on ) def test_given_json_params_then_we_fail_to_extract_properly_color_fill(json_params, data): - with pytest.raises(InvalidParamException, match=f"Invalid color fill '{data}'"): + with pytest.raises(InvalidParamError, match=f"Invalid color fill '{data}'"): color_fill_string_to_value(json_params) def test_given_invalid_color_fill_then_get_an_invalid_param_exception(): - with pytest.raises(InvalidParamException, match="Invalid color fill 'bla'"): + with pytest.raises(InvalidParamError, match="Invalid color fill 'bla'"): color_fill_string_to_value({"color_fill": "bla"}) diff --git a/pyproject.toml b/pyproject.toml index 12c9bfdcb..8dda081bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,9 +44,7 @@ select = ["E", "F", "I", "D", "N", "W", # To be added: # "SIM", "UP", "ANN", "B" ] -ignore = ["E203", "D100", "D202", "D407", "ANN101", "ANN102", "ANN204", - # To be fixed: - "I001", "E721", "N818"] +ignore = ["E203", "D100", "D202", "D407", "ANN101", "ANN102", "ANN204"] fixable = ["ALL"]