66>>> from phonenumbers.pb2 import phonenumber_pb2, PBToPy, PyToPB
77>>> x_py = phonenumbers.PhoneNumber(country_code=44, national_number=7912345678)
88>>> print x_py
9- Country Code: 44 National Number: 7912345678 Leading Zero: False
9+ Country Code: 44 National Number: 7912345678
1010>>> y_pb = phonenumber_pb2.PhoneNumber()
1111>>> y_pb.country_code = 44
1212>>> y_pb.national_number = 7912345678
1313>>> print str(y_pb).strip()
1414country_code: 44
1515national_number: 7912345678
16- >>> # Although italian_leading_zero is not set and doesn't appear in string representation
16+ >>> # Check italian_leading_zero default value when not set
1717>>> y_pb.italian_leading_zero
1818False
1919>>> y_py = PBToPy(y_pb)
2020>>> print y_py
21- Country Code: 44 National Number: 7912345678 Leading Zero: False
21+ Country Code: 44 National Number: 7912345678
2222>>> x_pb = PyToPB(x_py)
2323>>> print str(x_pb).strip()
2424country_code: 44
2525national_number: 7912345678
26- italian_leading_zero: false
2726>>> x_py == y_py
2827True
29- >>> # Protobuf versions are *not* equal, because one has False and one has (unset) for italian_leading_zero
3028>>> x_pb == y_pb
31- False
32- >>> # Explicitly set the field
29+ True
30+ >>> # Explicitly set the field to its default
3331>>> y_pb.italian_leading_zero = y_pb.italian_leading_zero
3432>>> x_pb == y_pb
35- True
33+ False
3634"""
3735
3836from phonenumber_pb2 import PhoneNumber as PhoneNumberPB
@@ -43,7 +41,7 @@ def PBToPy(numpb):
4341 return PhoneNumber (numpb .country_code if numpb .HasField ("country_code" ) else None ,
4442 numpb .national_number if numpb .HasField ("national_number" ) else None ,
4543 numpb .extension if numpb .HasField ("extension" ) else None ,
46- numpb .italian_leading_zero if numpb .HasField ("italian_leading_zero" ) else False ,
44+ numpb .italian_leading_zero if numpb .HasField ("italian_leading_zero" ) else None ,
4745 numpb .raw_input if numpb .HasField ("raw_input" ) else None ,
4846 numpb .country_code_source if numpb .HasField ("country_code_source" ) else None ,
4947 numpb .preferred_domestic_carrier_code if numpb .HasField ("preferred_domestic_carrier_code" ) else None )
@@ -57,10 +55,8 @@ def PyToPB(numobj):
5755 numpb .national_number = numobj .national_number
5856 if numobj .extension is not None :
5957 numpb .extension = numobj .extension
60- # For italian_leading_zero, the Python object has two states (True/False),
61- # but the protobuf version has three states (True/False/NotSet), and the
62- # NotSet state is effectively False.
63- numpb .italian_leading_zero = numobj .italian_leading_zero
58+ if numobj .italian_leading_zero is not None :
59+ numpb .italian_leading_zero = numobj .italian_leading_zero
6460 if numobj .raw_input is not None :
6561 numpb .raw_input = numobj .raw_input
6662 if numobj .country_code_source is not None :
0 commit comments