Repository navigation
fix(anonymizer): stop REMOVE_INTERSECTIONS from leaving flagged text in clear - #2331
Conversation
omri374
left a comment
There was a problem hiding this comment.
The fix is correct. The new test fails on main and passes here. Fuzzing 60k random result sets through the engine: on main, 201 sets left flagged characters uncovered and 442 returned zero-length results; on this branch there are no overlaps, no zero-length output and no uncovered flagged characters. ruff check is clean.
Inline comments cover one gap worth closing before merge (the < filter change has no test) and three non-blocking cleanups.
One item is outside the diff: presidio_anonymizer/entities/conflict_resolution_strategy.py holds the only user-facing description of REMOVE_INTERSECTIONS, and it does not say that lower-scored overlaps are trimmed or that results trimmed to nothing are dropped. Since the PR declares this as a behavior change, a sentence or two in that docstring would keep docs in step with the code (the documentation checkbox in the PR template is also unchecked).
Generated by Claude Code
… REMOVE_INTERSECTIONS trimming
|
Thanks for the review. 36124f5 adds the fully-consumed test case and updates the |
|
The red E2E (amd64) job is not from this change. That job's pip cache also restores |
Change Description
With
ConflictResolutionStrategy.REMOVE_INTERSECTIONS, the trimming pass sorts results bystartonly. After a trim, two results can share a start, and when the longer one ends up first it is compared with the shorter one and trimmed to zero length. Its tail is then left unanonymized, and an empty placeholder is still inserted:The fix sorts by
(start, end)in both places, so the shorter result is resolved first and the longer one is trimmed after it, and drops zero-length results (start < endinstead ofstart <= end) so no empty placeholder is emitted.Behavior change: for
REMOVE_INTERSECTIONSonly, results that were cut to zero length are now kept as non-empty spans after the higher-priority result, and zero-length results are no longer returned. In a randomized check (150k sets of 2-5 random overlapping results over a 30-character text), main left some flagged characters uncovered in 1,579 sets and returned 2,751 zero-length results; with the fix, none of either. Other strategies are unchanged.Issue reference
No existing issue. Open PR #2287 touches the same block (new opt-in strategy), so one of the two will need a small rebase.
Checklist
Two new cases in
test_when_remove_intersections_conflict_selected_then_all_conflicts_handled: one fails on main, the other fails if only the filter is reverted to<=. TheREMOVE_INTERSECTIONSdocstring now describes the trimming. presidio-anonymizer tests: 318 passed (test_ahds_surrogate.pyskipped,dotenvnot installed here).ruff checkclean;ruff format --checkflags two pre-existing lines inanonymizer_engine.pythat this PR does not touch.AI assistance (Claude Code) was used for this change.