From 44fb1b85c0251725d61f54a23f62d898ac4108ab Mon Sep 17 00:00:00 2001 From: David Drysdale Date: Thu, 2 Sep 2021 10:27:06 +0100 Subject: [PATCH] Generate .pyi files from metadata too Contents of generated files taken from #207 --- python/phonenumbers/carrierdata/__init__.pyi | 2 ++ python/phonenumbers/data/__init__.pyi | 9 ++++++++ python/phonenumbers/geodata/__init__.pyi | 2 ++ python/phonenumbers/geodata/locale.pyi | 1 + python/phonenumbers/shortdata/__init__.pyi | 3 +++ python/phonenumbers/tzdata/__init__.pyi | 2 ++ python/tests/testcarrierdata/__init__.pyi | 2 ++ python/tests/testdata/__init__.pyi | 5 +++++ python/tests/testgeodata/__init__.pyi | 2 ++ python/tests/testtzdata/__init__.pyi | 2 ++ tools/python/buildmetadatafromxml.py | 22 +++++++++++++++++++- tools/python/buildprefixdata.py | 8 +++++++ tools/python/makefile | 4 +++- 13 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 python/phonenumbers/carrierdata/__init__.pyi create mode 100644 python/phonenumbers/data/__init__.pyi create mode 100644 python/phonenumbers/geodata/__init__.pyi create mode 100644 python/phonenumbers/geodata/locale.pyi create mode 100644 python/phonenumbers/shortdata/__init__.pyi create mode 100644 python/phonenumbers/tzdata/__init__.pyi create mode 100644 python/tests/testcarrierdata/__init__.pyi create mode 100644 python/tests/testdata/__init__.pyi create mode 100644 python/tests/testgeodata/__init__.pyi create mode 100644 python/tests/testtzdata/__init__.pyi diff --git a/python/phonenumbers/carrierdata/__init__.pyi b/python/phonenumbers/carrierdata/__init__.pyi new file mode 100644 index 000000000..e47cb065e --- /dev/null +++ b/python/phonenumbers/carrierdata/__init__.pyi @@ -0,0 +1,2 @@ +CARRIER_DATA: dict[str, dict[str, str]] +CARRIER_LONGEST_PREFIX: int diff --git a/python/phonenumbers/data/__init__.pyi b/python/phonenumbers/data/__init__.pyi new file mode 100644 index 000000000..0f69d5c65 --- /dev/null +++ b/python/phonenumbers/data/__init__.pyi @@ -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, ...]] diff --git a/python/phonenumbers/geodata/__init__.pyi b/python/phonenumbers/geodata/__init__.pyi new file mode 100644 index 000000000..0ddf83d19 --- /dev/null +++ b/python/phonenumbers/geodata/__init__.pyi @@ -0,0 +1,2 @@ +GEOCODE_DATA: dict[str, dict[str, str]] +GEOCODE_LONGEST_PREFIX: int diff --git a/python/phonenumbers/geodata/locale.pyi b/python/phonenumbers/geodata/locale.pyi new file mode 100644 index 000000000..11391e542 --- /dev/null +++ b/python/phonenumbers/geodata/locale.pyi @@ -0,0 +1 @@ +LOCALE_DATA: dict[str, dict[str, str]] diff --git a/python/phonenumbers/shortdata/__init__.pyi b/python/phonenumbers/shortdata/__init__.pyi new file mode 100644 index 000000000..164b9f8a1 --- /dev/null +++ b/python/phonenumbers/shortdata/__init__.pyi @@ -0,0 +1,3 @@ +_AVAILABLE_REGION_CODES: list[str] + +def _load_region(code: str) -> None: ... diff --git a/python/phonenumbers/tzdata/__init__.pyi b/python/phonenumbers/tzdata/__init__.pyi new file mode 100644 index 000000000..45d621a7a --- /dev/null +++ b/python/phonenumbers/tzdata/__init__.pyi @@ -0,0 +1,2 @@ +TIMEZONE_DATA: dict[str, tuple[str, ...]] +TIMEZONE_LONGEST_PREFIX: int diff --git a/python/tests/testcarrierdata/__init__.pyi b/python/tests/testcarrierdata/__init__.pyi new file mode 100644 index 000000000..e47cb065e --- /dev/null +++ b/python/tests/testcarrierdata/__init__.pyi @@ -0,0 +1,2 @@ +CARRIER_DATA: dict[str, dict[str, str]] +CARRIER_LONGEST_PREFIX: int diff --git a/python/tests/testdata/__init__.pyi b/python/tests/testdata/__init__.pyi new file mode 100644 index 000000000..15e9a5dfa --- /dev/null +++ b/python/tests/testdata/__init__.pyi @@ -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, ...]] diff --git a/python/tests/testgeodata/__init__.pyi b/python/tests/testgeodata/__init__.pyi new file mode 100644 index 000000000..0ddf83d19 --- /dev/null +++ b/python/tests/testgeodata/__init__.pyi @@ -0,0 +1,2 @@ +GEOCODE_DATA: dict[str, dict[str, str]] +GEOCODE_LONGEST_PREFIX: int diff --git a/python/tests/testtzdata/__init__.pyi b/python/tests/testtzdata/__init__.pyi new file mode 100644 index 000000000..45d621a7a --- /dev/null +++ b/python/tests/testtzdata/__init__.pyi @@ -0,0 +1,2 @@ +TIMEZONE_DATA: dict[str, tuple[str, ...]] +TIMEZONE_LONGEST_PREFIX: int diff --git a/tools/python/buildmetadatafromxml.py b/tools/python/buildmetadatafromxml.py index 7398523a4..3412ee9f4 100755 --- a/tools/python/buildmetadatafromxml.py +++ b/tools/python/buildmetadatafromxml.py @@ -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: @@ -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) @@ -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.""" diff --git a/tools/python/buildprefixdata.py b/tools/python/buildprefixdata.py index 86526a022..5bc780ca9 100755 --- a/tools/python/buildprefixdata.py +++ b/tools/python/buildprefixdata.py @@ -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: diff --git a/tools/python/makefile b/tools/python/makefile index 2f900b681..b0380eb6d 100644 --- a/tools/python/makefile +++ b/tools/python/makefile @@ -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: