Skip to content

EngineResult equality ignores extra items, so results with different items compare equal #2488

Open

Description

@1AbdullahButt

Describe the bug
EngineResult.__eq__ returns True for two results with the same text but a different number of items. It compares the items with map(lambda x, y: x == y, self.items, other.items), and map stops at the shorter list, so extra items are never compared. A result with no items is equal to any result with the same text.

Code:

def __eq__(self, other) -> bool:
"""Verify two instances are equal.
Returns true if the two instances are equal, false otherwise.
"""
return self.text == other.text and all(
map(lambda x, y: x == y, self.items, other.items)
)

To Reproduce
On main (aa77b9f):

from presidio_anonymizer.entities import EngineResult, OperatorResult

one = EngineResult("x", [OperatorResult(0, 1, "PERSON", "x", "replace")])
two = EngineResult("x", [
    OperatorResult(0, 1, "PERSON", "x", "replace"),
    OperatorResult(5, 9, "EMAIL_ADDRESS", "y", "replace"),
])
print(one == two)                   # True
print(EngineResult("x", []) == two) # True

Expected behavior
Both print False.

Actual behavior
Both print True. A test that compares an anonymizer result with == passes even when an item is missing or extra.

Additional context
Comparing the lengths first (or comparing self.items == other.items) fixes it.

I have a fix with a regression test and will open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions