Skip to content
Merged
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
2 changes: 2 additions & 0 deletions python/phonenumbers/carrierdata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CARRIER_DATA: dict[str, dict[str, str]]
CARRIER_LONGEST_PREFIX: int
9 changes: 9 additions & 0 deletions python/phonenumbers/data/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from ..phonemetadata import NumberFormat

_AVAILABLE_REGION_CODES: list[str]
_AVAILABLE_NONGEO_COUNTRY_CODES: list[int]

def _load_region(code: str | int) -> None: ...

_ALT_NUMBER_FORMATS: dict[int, list[NumberFormat]]
_COUNTRY_CODE_TO_REGION_CODE: dict[int, tuple[str, ...]]
2 changes: 2 additions & 0 deletions python/phonenumbers/geodata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GEOCODE_DATA: dict[str, dict[str, str]]
GEOCODE_LONGEST_PREFIX: int
1 change: 1 addition & 0 deletions python/phonenumbers/geodata/locale.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LOCALE_DATA: dict[str, dict[str, str]]
3 changes: 3 additions & 0 deletions python/phonenumbers/shortdata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_AVAILABLE_REGION_CODES: list[str]

def _load_region(code: str) -> None: ...
2 changes: 2 additions & 0 deletions python/phonenumbers/tzdata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TIMEZONE_DATA: dict[str, tuple[str, ...]]
TIMEZONE_LONGEST_PREFIX: int
2 changes: 2 additions & 0 deletions python/tests/testcarrierdata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CARRIER_DATA: dict[str, dict[str, str]]
CARRIER_LONGEST_PREFIX: int
5 changes: 5 additions & 0 deletions python/tests/testdata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_AVAILABLE_REGION_CODES: list[str]
_AVAILABLE_NONGEO_COUNTRY_CODES: list[int]

def _load_region(code: str | int) -> None: ...
_COUNTRY_CODE_TO_REGION_CODE: dict[int, tuple[str, ...]]
2 changes: 2 additions & 0 deletions python/tests/testgeodata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GEOCODE_DATA: dict[str, dict[str, str]]
GEOCODE_LONGEST_PREFIX: int
2 changes: 2 additions & 0 deletions python/tests/testtzdata/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TIMEZONE_DATA: dict[str, tuple[str, ...]]
TIMEZONE_LONGEST_PREFIX: int
22 changes: 21 additions & 1 deletion tools/python/buildmetadatafromxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Processes the given XML metadata file and emit generated Python code.
The output directory will be created if it does not exist, and
__init__.py and per-region data files will be created in the directory.
__init__.py[i] and per-region data files will be created in the directory.
"""

# Based on original Java code and XML file:
Expand Down Expand Up @@ -72,6 +72,11 @@ def _load_region(code):
for region_code in _AVAILABLE_REGION_CODES:
PhoneMetadata.register_%(prefix)sregion_loader(region_code, _load_region)
'''
METADATA_FILE_TYPE_IMPORT = "from %(module)s.phonemetadata import NumberFormat\n"
METADATA_FILE_TYPE_INFO = '''
def _load_region(code: str) -> None: ...'''
METADATA_FILE_TYPE_INFO_WITH_NONGEO = '''
def _load_region(code: str | int) -> None: ...'''
METADATA_NONGEO_FILE_LOOP = '''
for country_code in _AVAILABLE_NONGEO_COUNTRY_CODES:
PhoneMetadata.register_nongeo_region_loader(country_code, _load_region)
Expand Down Expand Up @@ -668,6 +673,21 @@ def emit_metadata_py(self, datadir, module_prefix):
prnt(' %d: ("%s",),' % (country_code, '", "'.join(country_ids)), file=outfile)
prnt("}", file=outfile)

# Emit corresponding typing info
with open(modulefilename + "i", "w") as pyifile:
if self.alt_territory is not None:
prnt(METADATA_FILE_TYPE_IMPORT % {'module': module_prefix}, file=pyifile)
prnt("_AVAILABLE_REGION_CODES: list[str]", file=pyifile)
if len(nongeo_codes) > 0:
prnt("_AVAILABLE_NONGEO_COUNTRY_CODES: list[int]", file=pyifile)
prnt(METADATA_FILE_TYPE_INFO_WITH_NONGEO, file=pyifile)
else:
prnt(METADATA_FILE_TYPE_INFO, file=pyifile)
if self.alt_territory is not None:
prnt("\n_ALT_NUMBER_FORMATS: dict[int, list[NumberFormat]]", file=pyifile)
if len(country_code_to_region_code.keys()) > 0:
prnt("_COUNTRY_CODE_TO_REGION_CODE: dict[int, tuple[str, ...]]", file=pyifile)


def _standalone(argv):
"""Parse the given XML file and emit generated code."""
Expand Down
8 changes: 8 additions & 0 deletions tools/python/buildprefixdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ def output_prefixdata_code(prefixdata, outfilename, module_prefix, varprefix, pe
prnt("del data", file=outfile)
prnt("%s_LONGEST_PREFIX = %d" % (varprefix, longest_prefix), file=outfile)

# Emit corresponding typing info.
with open(outfilename + "i", "w") as pyifile:
if per_locale:
prnt("%s_DATA: dict[str, dict[str, str]]" % varprefix, file=pyifile)
else:
prnt("%s_DATA: dict[str, tuple[str, ...]]" % varprefix, file=pyifile)
prnt("%s_LONGEST_PREFIX: int" % varprefix, file=pyifile)


def output_prefixdata_chunk(prefixdata, outfilename, module_prefix, per_locale):
with open(outfilename, "w") as outfile:
Expand Down
4 changes: 3 additions & 1 deletion tools/python/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ DumpLocale.class: DumpLocale.java
javac $<
$(PYDIR)/phonenumbers/geodata/locale.py: DumpLocale.class | $(PYDIR)/phonenumbers/geodata
java DumpLocale > $@
locale: $(PYDIR)/phonenumbers/geodata/locale.py
$(PYDIR)/phonenumbers/geodata/locale.pyi:
echo "LOCALE_DATA: dict[str, dict[str, str]]" > $@
locale: $(PYDIR)/phonenumbers/geodata/locale.py $(PYDIR)/phonenumbers/geodata/locale.pyi

# Generate Python files from geocoding data
$(PYDIR)/phonenumbers/geodata:
Expand Down