Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add aio support #217

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 18 additions & 6 deletions mypy_protobuf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ def __init__(
self.from_imports: Dict[str, Set[Tuple[str, Optional[str]]]] = defaultdict(set)
self.locals: Set[str] = set()

def _import(self, path: str, name: str) -> str:
def _import(self, path: str, name: str, alias: Optional[str] = None) -> str:
"""Imports a stdlib path and returns a handle to it
eg. self._import("typing", "Optional") -> "Optional"
"""
imp = path.replace("/", ".")
if self.readable_stubs:
self.from_imports[imp].add((name, None))
return name
self.from_imports[imp].add((name, alias))
return alias or name
else:
self.imports.add(imp)
return imp + "." + name
Expand Down Expand Up @@ -656,7 +656,9 @@ def write_grpc_stub_methods(self, service: d.ServiceDescriptorProto) -> None:
l("{}] = ...", self._output_type(method, False))
l("")

def write_grpc_services(self, services: Iterable[d.ServiceDescriptorProto]) -> None:
def write_grpc_services(
self, services: Iterable[d.ServiceDescriptorProto], use_aio: bool
) -> None:
l = self._write_line
l(
"from .{} import *",
Expand All @@ -682,11 +684,19 @@ def write_grpc_services(self, services: Iterable[d.ServiceDescriptorProto]) -> N
with self._indent():
self.write_grpc_methods(service)
l("")
if use_aio:
server_type_hint = "{}[{}, {}]".format(
self._import("typing", "Union"),
self._import("grpc", "Server"),
self._import("grpc.aio", "Server", "AioServer"),
)
else:
server_type_hint = "{}".format(self._import("grpc", "Server"))
l(
"def add_{}Servicer_to_server(servicer: {}Servicer, server: {}) -> None: ...",
service.name,
service.name,
self._import("grpc", "Server"),
server_type_hint,
)
l("")

Expand Down Expand Up @@ -802,12 +812,13 @@ def generate_mypy_grpc_stubs(
quiet: bool,
readable_stubs: bool,
relax_strict_optional_primitives: bool,
use_aio: bool,
) -> None:
for name, fd in descriptors.to_generate.items():
pkg_writer = PkgWriter(
fd, descriptors, readable_stubs, relax_strict_optional_primitives
)
pkg_writer.write_grpc_services(fd.service)
pkg_writer.write_grpc_services(fd.service, use_aio)

assert name == fd.name
assert fd.name.endswith(".proto")
Expand Down Expand Up @@ -867,6 +878,7 @@ def grpc() -> None:
"quiet" in request.parameter,
"readable_stubs" in request.parameter,
"relax_strict_optional_primitives" in request.parameter,
"use_aio" in request.parameter,
)


Expand Down