Repository navigation
Fix KeyError for unknown CN RIC birth place codes - #524
Open
36taransingh5-dotcom wants to merge 2 commits into
Open
36taransingh5-dotcom wants to merge 2 commits into
36taransingh5-dotcom wants to merge 2 commits into
Conversation
get_birth_place() crashed with KeyError: 'county' when the location database matched the province prefix but not the full region code, so validate() leaked a non-ValidationError and is_valid() raised instead of returning False. Reject such numbers with InvalidComponent like other unknown birth place codes.
Cover a number with a known province prefix but an unknown county code: validate() must raise InvalidComponent and is_valid() must return False instead of raising KeyError.
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.
stdnum.cn.riccrashes with a rawKeyError: 'county'for RIC numbers whose region code has a known province prefix but is not present in the location database (stdnum/cn/loc.dat). For examplevalidate('361426199101010072')raisesKeyErrorinstead of aValidationErrorsubclass, andis_valid()propagates the exception instead of returningFalse, contrary to the documented contract that invalid numbers are rejected with exceptions fromstdnum.exceptions.The cause is in
get_birth_place(): whennumdb.get('cn/loc').info()returns a partial match (province found, county code unknown), the merged info dict has nocountykey andinfo['county']raisesKeyError.The fix rejects such numbers with
InvalidComponent, the same exception already used for fully unknown or no-longer-assigned birth place codes. The new check on thecountykey subsumes the previousif not inforejection.Regression tests added in
tests/test_cn_ric.doctestcover bothvalidate()raisingInvalidComponentandis_valid()returningFalsefor the affected input; they fail on current master with theKeyErrorand pass with this change. The full test suite passes locally (413 passed, 9 skipped, 100% coverage as required) and flake8 is clean on the changed file.Note: this patch was prepared with AI assistance and reviewed and tested locally before submission.