Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions python/phonenumbers/prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,5 @@ def _prefix_description_for_number(data, longest_prefix, numobj, lang, script=No
name = _find_lang(data[prefix], lang, script, region)
if name is not None:
return name
else:
return U_EMPTY_STRING
# Language not available for this prefix; try a less-specific (shorter) prefix.
return U_EMPTY_STRING
24 changes: 24 additions & 0 deletions python/tests/geocodertest.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ def testGetDescriptionForNonGeographicalNumberWithGeocodingPrefix(self):
# We have a geocoding prefix, but we shouldn't use it since this is not geographical.
self.assertEqual("South Korea", description_for_number(KO_MOBILE, _ENGLISH))

def testPrefixFallsBackToShorterPrefixWhenLanguageMissing(self):
# When a longer prefix exists in the geocoding data but lacks the requested

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you add "Python version extra test" at the top of the comment as reminder that this has no equivalent Java test.

# language, the lookup must continue to shorter prefixes rather than returning
# empty immediately. This can occur when only a subset of sub-prefixes carry
# alternative-language entries for a region.
#
# Simulate: '1212' has 'en', '12123' exists only for 'de'.
# Asking for 'fr' on a number whose digits start with 12123 should fall back
# to '1212' and return the English name (since French falls back to English),
# not the empty string from '12123'.
TEST_GEOCODE_DATA['12123'] = {'de': u("TestDistrict")}
try:
# US_NUMBER3 is +1 212-812-0000; its prefix digits start with '12128' so
# it won't hit '12123'. Use a fabricated number whose digits begin '12123'.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the first sentence of the comment is unhelpful, please remove it.

test_num = FrozenPhoneNumber(country_code=1, national_number=2123000000)
# '12123' has no 'fr', '1212' has 'en' -> French should fall back to 'NY'
self.assertEqual("NY", _prefix_description_for_number(
TEST_GEOCODE_DATA, TEST_GEOCODE_LONGEST_PREFIX, test_num, "fr"))
# 'de' is directly present in '12123', so German gets the detailed name
self.assertEqual("TestDistrict", _prefix_description_for_number(
TEST_GEOCODE_DATA, TEST_GEOCODE_LONGEST_PREFIX, test_num, "de"))
finally:
del TEST_GEOCODE_DATA['12123']

def testCoverage(self):
# Python version extra tests
invalid_number = PhoneNumber(country_code=210, national_number=123456)
Expand Down