Skip to content

Commit b1be9e7

Browse files
roubertdaviddrysdale
authored andcommitted
Distinguish between None and False for italian_leading_zero
For better consistency with upstream Java code, keep track of the difference between not-set (None) and set-off (False)
1 parent 5003d7f commit b1be9e7

5 files changed

Lines changed: 23 additions & 13 deletions

File tree

python/phonenumbers/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
>>> from phonenumbers.util import prnt # equivalent to Py3k print()
77
>>> x = phonenumbers.parse("+442083661177", None)
88
>>> prnt(x)
9-
Country Code: 44 National Number: 2083661177 Leading Zero(s): False
9+
Country Code: 44 National Number: 2083661177
1010
>>> type(x)
1111
<class 'phonenumbers.phonenumber.PhoneNumber'>
1212
>>> str(phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.NATIONAL))
@@ -17,7 +17,7 @@
1717
'+442083661177'
1818
>>> y = phonenumbers.parse("020 8366 1177", "GB")
1919
>>> prnt(y)
20-
Country Code: 44 National Number: 2083661177 Leading Zero(s): False
20+
Country Code: 44 National Number: 2083661177
2121
>>> x == y
2222
True
2323
>>>

python/phonenumbers/phonenumber.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self,
5757
country_code=None,
5858
national_number=None,
5959
extension=None,
60-
italian_leading_zero=False,
60+
italian_leading_zero=None,
6161
number_of_leading_zeros=None,
6262
raw_input=None,
6363
country_code_source=None,
@@ -119,8 +119,18 @@ def __init__(self,
119119
#
120120
# Clients who use the parsing functionality of the i18n phone number
121121
# libraries will have these fields set if necessary automatically.
122-
self.italian_leading_zero = bool(italian_leading_zero)
123-
self.number_of_leading_zeros = number_of_leading_zeros # None or int
122+
#
123+
# None if not set, of type bool otherwise:
124+
if italian_leading_zero is None:
125+
self.italian_leading_zero = None
126+
else:
127+
self.italian_leading_zero = bool(italian_leading_zero)
128+
129+
# None if not set, of type int otherwise.
130+
if number_of_leading_zeros is None:
131+
self.number_of_leading_zeros = None
132+
else:
133+
self.number_of_leading_zeros = int(number_of_leading_zeros)
124134

125135
# The next few fields are non-essential fields for a phone number.
126136
# They retain extra information about the form the phone number was
@@ -156,7 +166,7 @@ def clear(self):
156166
self.country_code = None
157167
self.national_number = None
158168
self.extension = None
159-
self.italian_leading_zero = False
169+
self.italian_leading_zero = None
160170
self.number_of_leading_zeros = None
161171
self.raw_input = None
162172
self.country_code_source = None
@@ -187,7 +197,7 @@ def __eq__(self, other):
187197
return (self.country_code == other.country_code and
188198
self.national_number == other.national_number and
189199
self.extension == other.extension and
190-
self.italian_leading_zero == other.italian_leading_zero and
200+
bool(self.italian_leading_zero) == bool(other.italian_leading_zero) and
191201
self.number_of_leading_zeros == other.number_of_leading_zeros and
192202
self.raw_input == other.raw_input and
193203
self.country_code_source == other.country_code_source and
@@ -231,7 +241,7 @@ def __hash__(self):
231241
return hash((self.country_code,
232242
self.national_number,
233243
self.extension,
234-
self.italian_leading_zero,
244+
bool(self.italian_leading_zero),
235245
self.number_of_leading_zeros,
236246
self.raw_input,
237247
self.country_code_source,

python/tests/examplenumberstest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def testBlankMetadata(self):
234234
# Some metadata is blank; check that we cope with this.
235235
# Example: MH (+692)
236236
number = phonenumberutil.parse("+6927654321", "US")
237-
self.assertEqual("Country Code: 692 National Number: 7654321 Leading Zero(s): False", str(number))
237+
self.assertEqual("Country Code: 692 National Number: 7654321", str(number))
238238

239239
def testMetadataPrint(self):
240240
for callingCode in PhoneMetadata._region_available.keys():

python/tests/phonenumbermatchertest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def testStringConvert(self):
9696
# Python version extra test
9797
self.assertEqual("PhoneNumberMatch(start=10, raw_string='1 800 234 45 67', "
9898
"numobj=PhoneNumber(country_code=None, national_number=None, extension=None, "
99-
"italian_leading_zero=False, number_of_leading_zeros=None, "
99+
"italian_leading_zero=None, number_of_leading_zeros=None, "
100100
"country_code_source=None, preferred_domestic_carrier_code=None))", repr(match))
101101

102102

python/tests/phonenumberutiltest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,10 @@ def testFormatWithPreferredCarrierCode(self):
617617
phonenumbers.format_national_number_with_preferred_carrier_code(arNumber, ""))
618618
# Python version extra test: check string conversion with preferred carrier code
619619
self.assertEqual('Country Code: 54 National Number: 91234125678 '
620-
'Leading Zero(s): False Preferred Domestic Carrier Code: 19',
620+
'Preferred Domestic Carrier Code: 19',
621621
str(arNumber))
622622
self.assertEqual("PhoneNumber(country_code=54, national_number=91234125678, extension=None, "
623-
"italian_leading_zero=False, number_of_leading_zeros=None, "
623+
"italian_leading_zero=None, number_of_leading_zeros=None, "
624624
"country_code_source=None, preferred_domestic_carrier_code='19')",
625625
repr(arNumber))
626626
# When the preferred_domestic_carrier_code is present (even when it
@@ -1486,7 +1486,7 @@ def testMaybeExtractCountryCode(self):
14861486
self.assertEqual(strippedNumber, numberToFill,
14871487
msg="Did not strip off the country calling code correctly.")
14881488
# Python version extra test covering string conversion with country_code_source present
1489-
self.assertEqual("Country Code: 1 National Number: None Leading Zero(s): False Country Code Source: 5",
1489+
self.assertEqual("Country Code: 1 National Number: None Country Code Source: 5",
14901490
str(number))
14911491
except NumberParseException:
14921492
e = sys.exc_info()[1]

0 commit comments

Comments
 (0)