fix: keep all digits when formatting 13-digit SP/MG voter ids#742
Open
gaoflow wants to merge 2 commits into
Open
fix: keep all digits when formatting 13-digit SP/MG voter ids#742gaoflow wants to merge 2 commits into
gaoflow wants to merge 2 commits into
Conversation
format_voter_id used hardcoded 12-digit forward slices, so a valid 13-digit voter id (the SP/MG edge case with a 9-digit sequential number) lost its last digit and showed the wrong federative union and verifying digits, even though is_valid accepts it. Slice length-aware: the federative union and verifying digits are always the last four characters (via the existing backward-indexing helpers), and the sequential number is everything before them. 12-digit output is unchanged; 13-digit ids now format losslessly.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #742 +/- ##
=======================================
Coverage 99.08% 99.08%
=======================================
Files 26 26
Lines 765 769 +4
=======================================
+ Hits 758 762 +4
Misses 7 7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
What this fixes
format_voter_idsilently corrupts valid 13-digit voter IDs (the SP/MG edge case, where the sequential number has 9 digits instead of 8).is_validalready accepts 13-digit IDs (_is_length_validallowslen == 13for UF01/02), and the parsing helpers_get_federative_union/_get_verifying_digitsindex backwards specifically because "the sequential_number can have eight or nine digits". Onlyformat_voter_idused hardcoded 12-digit forward slices (voter_id[:4], [4:8], [8:10], [10:12]), so it dropped the 13th digit and misplaced the trailing fields.The change
Make the slicing length-aware, reusing the existing helpers: the federative union and verifying digits are always the last four characters, and the sequential number is everything before them.
2776 2712 28 52).32445 6780 01 67), so the output minus spaces round-trips back to the input.Tests
Added
test_format_voter_id_special_caseusing the official 13-digit fixture (3244567800167, already asserted valid bytest_valid_special_case): it checks validity, the formatted output, and that the result round-trips losslessly. It fails before the change (the last digit is dropped) and passes after. Full suite: 172 tests pass;ruff format --checkandruff checkclean.I used an AI assistant to help investigate this under my direction, and I have reviewed and verified the change myself.