Open alex-ber opened 1 year ago
Hi @alex-ber, thanks for raising this issue, we will look into this. If you're interested in creaing a PR, we would be happy to review. It is also somewhat related to #1052 as Presidio does not support the addition of external operators due to the way the OperatorsFactory
is implemented.
Describe the bug I've created 2 custom operators:
and
I've imported them. In Encrypt2 I'm marking encrypted word with @@@ at the beginning and at the end. In Decrypt2 I'm removing @@@ at the beginning and at the end of the word before decryption.
The problem is in class presidio_anonomizer.operators.operators_factory.OperatorsFactory in the magic static method:
More precisely in the line
operators = Operator.__subclasses__()
. This line returns only immediate sub-classes. So, it's find Encrypt\Decrypt classes because they inherit immediately from Operator, but fails to find my Encrypt/Decrypt2 classes, because Operator is only their grandfather (Encrypt2->Encrypt->Operator, Decrypt2->Decrypt->Operator).To Reproduce Define Encrypt2\Decryp2 classes as written above. Run the following code (add required imports)
You will get 'presidio_anonymizer.entities.invalid_exception.InvalidParamException: Invalid operator class 'encrypt2'.'
Expected behavior No exception, correct work.
Screenshots
Additional context As work-arround my Encryp2/Decrypt2 inherits from Operator directly and I copy&pasted code form Operator. So, for now, it's look like this:
and
Above is work-around. It's works.
Proposed code fix In OperatorsFactory change __get_operators_by_type() to take into account all sub-classes of Operator, not only immediate one. It is easy to do using recursion. Change&Add in OpratorsFactory to the following code: