Skip to content

Commit fc432cc

Browse files
authored
feat: code gen produces same imports as grpcio (#41)
without this change purerpc produces import statements of the form `import foo.bar.some_pb2 as foo_dot_bar_dot_some__pb2` whereas grpcio would produce `from foo.bar import some_pb2 as foo_dot_bar_dot_some__pb2` While equivalent where the import system is concerned, some tools and scripts exist for automatically fixing generated import statements (see for example https://github.com/cpcloud/protoletariat). These only work for import statements written in the latter form.
1 parent 30640d5 commit fc432cc

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/purerpc/protoc_plugin/plugin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
def generate_import_statement(proto_name):
1111
module_path = proto_name[:-len(".proto")].replace("-", "_").replace("/", ".") + "_pb2"
1212
alias = get_python_module_alias(proto_name)
13-
return "import " + module_path + " as " + alias
13+
if "." in module_path:
14+
# produces import statements in line with grpcio so that tools for
15+
# postprocessing import statements work with purerpc as well
16+
# example: `from foo.bar import zap_pb2 as foo_dot_bar_dot_zap__pb2`
17+
parent_modules, sub_module = module_path.rsplit(".", 1)
18+
return "from " + parent_modules + " import " + sub_module + " as " + alias
19+
else:
20+
return "import " + module_path + " as " + alias
1421

1522

1623
def get_python_module_alias(proto_name):

0 commit comments

Comments
 (0)