Skip to content

Commit 37ef497

Browse files
committed
Merge upstream code changes
1 parent 467636e commit 37ef497

6 files changed

Lines changed: 33 additions & 38 deletions

File tree

python/phonenumbers/phonemetadata.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@ def __init__(self,
159159
# its total length and leading digits.
160160
self.national_number_pattern = force_unicode(national_number_pattern) # None or Unicode string holding regexp
161161

162-
# The possible_number_pattern is now retired.
163-
self.possible_number_pattern = None
164-
165162
# An example national significant number for the specific type. It
166163
# should not contain any formatting information.
167164
self.example_number = force_unicode(example_number) # None or Unicode string
@@ -507,13 +504,8 @@ def __init__(self,
507504
# match.
508505
self.leading_digits = force_unicode(leading_digits) # None or Unicode string holding regexp
509506

510-
# The leading zero in a phone number is meaningful in some countries
511-
# (e.g. Italy). This means they cannot be dropped from the national
512-
# number when converting into international format. If leading zeros
513-
# are possible for valid international numbers for this region/country
514-
# then set this to true. This only needs to be set for the region
515-
# that is the main_country_for_code and all regions associated with
516-
# that calling code will use the same setting.
507+
# Deprecated: do not use. Will be deleted when there are no references
508+
# to this later.
517509
self.leading_zero_possible = bool(leading_zero_possible)
518510

519511
# This field is set when this country has implemented mobile number

python/phonenumbers/phonenumber.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
class CountryCodeSource(object):
2424
"""The source from which a country code is derived."""
25+
# Default value returned if this is not set, because the phone number was
26+
# created using parse(keep_raw_input=False).
27+
UNSPECIFIED = 0
2528

2629
# The country_code is derived based on a phone number with a leading "+",
2730
# e.g. the French number "+33 1 42 68 53 00".
@@ -60,7 +63,7 @@ def __init__(self,
6063
italian_leading_zero=None,
6164
number_of_leading_zeros=None,
6265
raw_input=None,
63-
country_code_source=None,
66+
country_code_source=CountryCodeSource.UNSPECIFIED,
6467
preferred_domestic_carrier_code=None):
6568
# The country calling code for this number, as defined by the
6669
# International Telecommunication Union (ITU). For example, this would
@@ -146,8 +149,9 @@ def __init__(self,
146149
# The source from which the country_code is derived. This is not set
147150
# in the general parsing method, but in the method that parses and
148151
# keeps raw_input. New fields could be added upon request.
149-
self.country_code_source = country_code_source
150-
# None or CountryCodeSource.VALUE
152+
self.country_code_source = country_code_source # CountryCodeSource.VALUE
153+
if self.country_code_source is None: # pragma no cover
154+
self.country_code_source = CountryCodeSource.UNSPECIFIED
151155

152156
# The carrier selection code that is preferred when calling this
153157
# phone number domestically. This also includes codes that need to
@@ -169,7 +173,7 @@ def clear(self):
169173
self.italian_leading_zero = None
170174
self.number_of_leading_zeros = None
171175
self.raw_input = None
172-
self.country_code_source = None
176+
self.country_code_source = CountryCodeSource.UNSPECIFIED
173177
self.preferred_domestic_carrier_code = None
174178

175179
def merge_from(self, other):
@@ -186,7 +190,7 @@ def merge_from(self, other):
186190
self.number_of_leading_zeros = other.number_of_leading_zeros
187191
if other.raw_input is not None:
188192
self.raw_input = other.raw_input
189-
if other.country_code_source is not None:
193+
if other.country_code_source is not CountryCodeSource.UNSPECIFIED:
190194
self.country_code_source = other.country_code_source
191195
if other.preferred_domestic_carrier_code is not None:
192196
self.preferred_domestic_carrier_code = other.preferred_domestic_carrier_code
@@ -227,7 +231,7 @@ def __unicode__(self):
227231
result += unicod(" Number of leading zeros: %d") % self.number_of_leading_zeros
228232
if self.extension is not None:
229233
result += unicod(" Extension: %s") % self.extension
230-
if self.country_code_source is not None:
234+
if self.country_code_source is not CountryCodeSource.UNSPECIFIED:
231235
result += unicod(" Country Code Source: %s") % self.country_code_source
232236
if self.preferred_domestic_carrier_code is not None:
233237
result += (unicod(" Preferred Domestic Carrier Code: %s") %

python/phonenumbers/phonenumbermatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def _parse_and_verify(self, candidate, offset):
690690
# TODO: stop clearing all values here and switch all users
691691
# over to using raw_input rather than the raw_string of
692692
# PhoneNumberMatch.
693-
numobj.country_code_source = None
693+
numobj.country_code_source = CountryCodeSource.UNSPECIFIED
694694
numobj.raw_input = None
695695
numobj.preferred_domestic_carrier_code = None
696696
return PhoneNumberMatch(offset, candidate, numobj)

python/phonenumbers/phonenumberutil.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,8 @@ def _formatting_rule_has_first_group_only(national_prefix_formatting_rule):
888888
def is_number_geographical(numobj):
889889
"""Tests whether a phone number has a geographical association.
890890
891-
It checks if the number is associated to a certain region in the country
892-
where it belongs to. Note that this doesn't verify if the number is
891+
It checks if the number is associated with a certain region in the country
892+
to which it belongs. Note that this doesn't verify if the number is
893893
actually in use.
894894
country_code -- the country calling code for which we want the mobile token
895895
"""
@@ -1315,8 +1315,7 @@ def format_in_original_format(numobj, region_calling_from):
13151315
PhoneNumber object passed in. If such information is missing, the number
13161316
will be formatted into the NATIONAL format by default.
13171317
1318-
When the number contains a leading zero and this is unexpected for this
1319-
country, or we don't have a formatting pattern for the number, the method
1318+
When we don't have a formatting pattern for the number, the method
13201319
returns the raw input when it is available.
13211320
13221321
Note this method guarantees no digit will be inserted, removed or modified
@@ -1330,12 +1329,11 @@ def format_in_original_format(numobj, region_calling_from):
13301329
13311330
Returns the formatted phone number in its original number format.
13321331
"""
1333-
if (numobj.raw_input is not None and
1334-
(_has_unexpected_italian_leading_zero(numobj) or not _has_formatting_pattern_for_number(numobj))):
1332+
if (numobj.raw_input is not None and not _has_formatting_pattern_for_number(numobj)):
13351333
# We check if we have the formatting pattern because without that, we
13361334
# might format the number as a group without national prefix.
13371335
return numobj.raw_input
1338-
if numobj.country_code_source is None:
1336+
if numobj.country_code_source is CountryCodeSource.UNSPECIFIED:
13391337
return format_number(numobj, PhoneNumberFormat.NATIONAL)
13401338

13411339
formatted_number = _format_original_allow_mods(numobj, region_calling_from)
@@ -2754,6 +2752,13 @@ def parse(number, region=None, keep_raw_input=False,
27542752
number is actually a valid number for a particular region is not
27552753
performed. This can be done separately with is_valid_number.
27562754
2755+
Note this method canonicalizes the phone number such that different
2756+
representations can be easily compared, no matter what form it was
2757+
originally entered in (e.g. national, international). If you want to
2758+
record context about the number being parsed, such as the raw input that
2759+
was entered, how the country code was derived etc. then ensure
2760+
keep_raw_input is set.
2761+
27572762
Note if any new field is added to this method that should always be filled
27582763
in, even when keep_raw_input is False, it should also be handled in the
27592764
_copy_core_fields_only() function.
@@ -2856,7 +2861,7 @@ def parse(number, region=None, keep_raw_input=False,
28562861
country_code = metadata.country_code
28572862
numobj.country_code = country_code
28582863
elif keep_raw_input:
2859-
numobj.country_code_source = None
2864+
numobj.country_code_source = CountryCodeSource.UNSPECIFIED
28602865

28612866
if len(normalized_national_number) < _MIN_LENGTH_FOR_NSN:
28622867
raise NumberParseException(NumberParseException.TOO_SHORT_NSN,

python/tests/phonenumbermatchertest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def testStringConvert(self):
9797
self.assertEqual("PhoneNumberMatch(start=10, raw_string='1 800 234 45 67', "
9898
"numobj=PhoneNumber(country_code=None, national_number=None, extension=None, "
9999
"italian_leading_zero=None, number_of_leading_zeros=None, "
100-
"country_code_source=None, preferred_domestic_carrier_code=None))", repr(match))
100+
"country_code_source=0, preferred_domestic_carrier_code=None))", repr(match))
101101

102102

103103
class NumberContext(object):

python/tests/phonenumberutiltest.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,6 @@ def testIsNumberGeographical(self):
201201
self.assertTrue(phonenumberutil.is_number_geographical(MX_MOBILE1)) # Mexico, mobile phone number.
202202
self.assertTrue(phonenumberutil.is_number_geographical(MX_MOBILE2)) # Mexico, another mobile phone number.
203203

204-
def testIsLeadingZeroPossible(self):
205-
self.assertTrue(phonenumberutil._is_leading_zero_possible(39)) # Italy
206-
self.assertFalse(phonenumberutil._is_leading_zero_possible(1)) # USA
207-
self.assertTrue(phonenumberutil._is_leading_zero_possible(800)) # International toll free
208-
self.assertFalse(phonenumberutil._is_leading_zero_possible(979)) # International premium-rate
209-
self.assertFalse(phonenumberutil._is_leading_zero_possible(888)) # Not in metadata file, just default to False
210-
211204
def testGetLengthOfGeographicalAreaCode(self):
212205
# Google MTV, which has area code "650".
213206
self.assertEqual(3, phonenumbers.length_of_geographical_area_code(US_NUMBER))
@@ -712,7 +705,7 @@ def testFormatWithPreferredCarrierCode(self):
712705
str(arNumber))
713706
self.assertEqual("PhoneNumber(country_code=54, national_number=91234125678, extension=None, "
714707
"italian_leading_zero=None, number_of_leading_zeros=None, "
715-
"country_code_source=None, preferred_domestic_carrier_code='19')",
708+
"country_code_source=0, preferred_domestic_carrier_code='19')",
716709
repr(arNumber))
717710
# When the preferred_domestic_carrier_code is present (even when it is
718711
# just a space), use it instead of the default carrier code passed in.
@@ -1896,8 +1889,7 @@ def testMaybeExtractCountryCode(self):
18961889
ccc, numberToFill = phonenumberutil._maybe_extract_country_code(phoneNumber, metadata, False, number)
18971890
self.assertEqual(countryCallingCode, ccc,
18981891
msg="Should have extracted the country calling code of the region passed in")
1899-
self.assertFalse(number.country_code_source is not None,
1900-
msg="Should not contain CountryCodeSource.")
1892+
self.assertEqual(CountryCodeSource.UNSPECIFIED, number.country_code_source)
19011893
except NumberParseException:
19021894
e = sys.exc_info()[1]
19031895
self.fail("Should not have thrown an exception: %s" % e)
@@ -1909,8 +1901,7 @@ def testMaybeExtractCountryCode(self):
19091901
self.assertEqual(0, ccc,
19101902
msg=("Should not have extracted a country calling code - invalid number after " +
19111903
"extraction of uncertain country calling code."))
1912-
self.assertFalse(number.country_code_source is not None,
1913-
msg="Should not contain CountryCodeSource.")
1904+
self.assertEqual(CountryCodeSource.UNSPECIFIED, number.country_code_source)
19141905
except NumberParseException:
19151906
e = sys.exc_info()[1]
19161907
self.fail("Should not have thrown an exception: %s" % e)
@@ -1931,6 +1922,9 @@ def testMaybeExtractCountryCode(self):
19311922
def testParseNationalNumber(self):
19321923
# National prefix attached.
19331924
self.assertEqual(NZ_NUMBER, phonenumbers.parse("033316005", "NZ"))
1925+
# Some fields are not filled in by parse when keep_raw_input is not set.
1926+
self.assertEqual(CountryCodeSource.UNSPECIFIED, NZ_NUMBER.country_code_source)
1927+
19341928
self.assertEqual(NZ_NUMBER, phonenumbers.parse("33316005", "NZ"))
19351929
# National prefix attached and some formatting present.
19361930
self.assertEqual(NZ_NUMBER, phonenumbers.parse("03-331 6005", "NZ"))

0 commit comments

Comments
 (0)