From c6b9f9fcf44c5cf4f80abd771cd0704710357818 Mon Sep 17 00:00:00 2001 From: Taran Pal Singh <36taransingh5@gmail.com> Date: Sat, 24 Oct 2026 10:10:41 +0100 Subject: [PATCH 1/2] Fix KeyError for unknown CN RIC birth place codes 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. --- stdnum/cn/ric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdnum/cn/ric.py b/stdnum/cn/ric.py index 12554b80..ed15cc80 100644 --- a/stdnum/cn/ric.py +++ b/stdnum/cn/ric.py @@ -65,7 +65,7 @@ def get_birth_place(number: str) -> dict[str, str]: number = compact(number) results = numdb.get('cn/loc').info(number[:6]) info = {k: v for part, props in results for k, v in props.items()} - if not info: + if 'county' not in info: raise InvalidComponent() year = get_birth_date(number).year # check if any found county was assigned at the birth year From 121ab66f0f0d4f1497f63f41270dc446fa5b0edb Mon Sep 17 00:00:00 2001 From: Taran Pal Singh <36taransingh5@gmail.com> Date: Sat, 24 Oct 2026 10:11:18 +0100 Subject: [PATCH 2/2] Add tests for unknown CN RIC 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. --- tests/test_cn_ric.doctest | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_cn_ric.doctest b/tests/test_cn_ric.doctest index ddd87802..d3ba9a4b 100644 --- a/tests/test_cn_ric.doctest +++ b/tests/test_cn_ric.doctest @@ -101,3 +101,9 @@ Invalid location: Traceback (most recent call last): ... InvalidComponent: ... +>>> ric.validate('361426199101010072') # known province, unknown county code +Traceback (most recent call last): + ... +InvalidComponent: ... +>>> ric.is_valid('361426199101010072') +False