Fix EsNieRecognizer.validate_result raising instead of returning False#2146
Merged
SharonHart merged 1 commit intoJul 7, 2026
Conversation
isdigit was referenced without being called, so the digit guard was always truthy and never actually checked anything. Under the recognizer's default pattern this stays hidden since the middle characters are already constrained to digits, but a custom patterns argument (which the constructor explicitly supports) can let non-digit text reach the checksum line further down and raise a ValueError instead of returning False. Fixes data-privacy-stack#2145
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a correctness bug in the Spain NIE predefined recognizer where validate_result() could raise ValueError (instead of returning False) when the recognizer is instantiated with a looser custom regex pattern.
Changes:
- Corrected the digit-guard in
EsNieRecognizer.validate_resultby callingisdigit()instead of referencing the method. - Added a regression test which configures
EsNieRecognizerwith a custom loose pattern and assertsanalyze()returns no results (and does not crash) on non-digit middle segments.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/spain/es_nie_recognizer.py | Fixes validate_result() digit validation so invalid matches are rejected without exceptions. |
| presidio-analyzer/tests/test_es_nie_recognizer.py | Adds regression coverage for the custom-pattern crash scenario described in #2145. |
SharonHart
approved these changes
Jul 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2145.
isdigitwas referenced without being called, so the digit guard invalidate_resultwas always truthy and never actually checked anything. The recognizer's default pattern already limits the middle characters to digits, so this stayed hidden in normal use, but the constructor explicitly accepts a custompatternsargument, and a looser pattern lets non-digit text reach the checksum line further down and raise aValueErrorinstead of returningFalse.The fix is one character, calling
isdigit()instead of referencing it.Added a regression test that builds the recognizer with a custom pattern loose enough to trigger the old crash, and asserts
analyze()now returns an empty list instead of raising.