Fix incorrect PESEL checksum validation in PlPeselRecognizer#1998
Merged
Conversation
The PESEL check-digit algorithm is `check = (10 - weighted_sum % 10) % 10` (https://en.wikipedia.org/wiki/PESEL#Check_digit). The previous implementation compared the raw `weighted_sum % 10` to the check digit, which incorrectly rejects valid PESEL numbers such as 44051401458 (the canonical example cited in official Polish documentation) and accepts nothing that a real PESEL-issuing authority would produce. This completes microsoft#1520, which was closed due to missing test coverage. Changes: - Correct the check-digit formula in `validate_result`. - Guard against non-11-digit / non-numeric inputs. - Replace the previous test fixtures (which relied on the buggy formula) with PESELs that are valid under the real algorithm, plus negative cases covering bad check digits, wrong length, and non-digit characters. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
Author
|
@microsoft-github-policy-service agree company="APIUS Technologies S.A." |
omri374
approved these changes
Apr 23, 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.
Change Description
The Polish PESEL check-digit algorithm is
check = (10 - weighted_sum % 10) % 10(see https://en.wikipedia.org/wiki/PESEL#Check_digit), but
PlPeselRecognizer.validate_resultcurrently compares the rawweighted_sum % 10to the check digit, which rejects real valid PESEL numbers.Example —
44051401458is the canonical example used in official Polishdocumentation and is algorithmically valid, but today:
This PR:
IndexErroron a regex-produced string shorter than 11).formula and would not pass real-world PESEL validators — with PESEL numbers
that are valid under the true algorithm, plus negative cases covering wrong
check digits, wrong length, and non-digit input.
The previous "valid" fixture
11111111114has a real check digit of6, not4. The updated fixtures (44051401458,02070803628,11111111116) verifyagainst the actual PESEL standard.
Issue reference
This completes the work started in #1520 (closed on 2025-06-01 due to missing
tests). Attribution to the original author @BlaiseCz for the analysis.
Checklist
pytest tests/test_pl_pesel_recognizer.py→ 18 passed)