Skip to content

Fix KeyError for unknown CN RIC birth place codes - #524

Open
36taransingh5-dotcom wants to merge 2 commits into
arthurdejong:masterfrom
36taransingh5-dotcom:patch-1
Open

36taransingh5-dotcom wants to merge 2 commits into
arthurdejong:masterfrom
36taransingh5-dotcom:patch-1

Conversation

@36taransingh5-dotcom

Copy link
Copy Markdown

stdnum.cn.ric crashes with a raw KeyError: '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 example validate('361426199101010072') raises KeyError instead of a ValidationError subclass, and is_valid() propagates the exception instead of returning False, contrary to the documented contract that invalid numbers are rejected with exceptions from stdnum.exceptions.

The cause is in get_birth_place(): when numdb.get('cn/loc').info() returns a partial match (province found, county code unknown), the merged info dict has no county key and info['county'] raises KeyError.

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 the county key subsumes the previous if not info rejection.

Regression tests added in tests/test_cn_ric.doctest cover both validate() raising InvalidComponent and is_valid() returning False for the affected input; they fail on current master with the KeyError and 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant