Skip to content

Commit 1492b68

Browse files
committed
refactor: py_import_func to return PyImportPath
Requires the py_import_func to return a PyImportPath directly instead of just a string. BREAKING CHANGE: This obviously breaks code that was provided a py_import_func returning a string.
1 parent bb1d541 commit 1492b68

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

protogen/__init__.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,14 +1202,11 @@ def __init__(
12021202
self,
12031203
proto: google.protobuf.descriptor_pb2.FileDescriptorProto,
12041204
generate: bool,
1205-
py_import_func: Callable[[str, str], str],
1205+
py_import_func: Callable[[str, str], PyImportPath],
12061206
):
12071207
self.proto = proto
12081208
self.generated_filename_prefix = proto.name[: -len(".proto")]
1209-
# The actual pyton path is determined using the py_import_func.
1210-
# TODO maybe require the rewrite func to return a Python file and
1211-
# then generate the import path from that python filename
1212-
self.py_import_path = PyImportPath(py_import_func(proto.name, proto.package))
1209+
self.py_import_path = py_import_func(proto.name, proto.package)
12131210
self.py_package_name = str(proto.package)
12141211
self.generate = generate
12151212
self.dependencies: List[File] = []
@@ -1533,7 +1530,7 @@ def error(self, msg: str):
15331530
self._error = msg
15341531

15351532

1536-
def default_py_import_func(filename: str, package: str) -> str:
1533+
def default_py_import_func(filename: str, package: str) -> PyImportPath:
15371534
"""Return the Python import path for a file.
15381535
15391536
Return the Python import path for a file following the behaviour of the
@@ -1551,15 +1548,15 @@ def default_py_import_func(filename: str, package: str) -> str:
15511548
15521549
Returns
15531550
-------
1554-
str
1551+
PyImportPath
15551552
The Python import path for the file.
15561553
15571554
Example
15581555
-------
15591556
>>> default_py_import_func("google/protobuf/field_mask.proto", "google.protobuf")
15601557
"google.protobuf.field_mask_pb2"
15611558
"""
1562-
return filename.replace(".proto", "_pb2").replace("/", ".")
1559+
return PyImportPath(filename.replace(".proto", "_pb2").replace("/", "."))
15631560

15641561

15651562
class Options:
@@ -1576,15 +1573,15 @@ class Options:
15761573
def __init__(
15771574
self,
15781575
*,
1579-
py_import_func: Callable[[str, str], str] = default_py_import_func,
1576+
py_import_func: Callable[[str, str], PyImportPath] = default_py_import_func,
15801577
input: BinaryIO = sys.stdin.buffer,
15811578
output: BinaryIO = sys.stdout.buffer,
15821579
):
15831580
"""Create options for the resolution process.
15841581
15851582
Arguments
15861583
---------
1587-
py_import_func : Callable[[str, str], str], optional
1584+
py_import_func : Callable[[str, str], PyImportPath], optional
15881585
Defines how to derive :class:`PyImportPath` for the :class:`File`
15891586
objects in the resolution process. This also influences the
15901587
:class:`PyIdent` attributes that are part of :class:`Message`,

0 commit comments

Comments
 (0)