Skip to content

Only import types from grpclib when type checking #52

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

Merged
merged 2 commits into from
May 24, 2020
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
14 changes: 8 additions & 6 deletions betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
TYPE_CHECKING,
)

import grpclib.client

import grpclib.const
import stringcase

from .casing import safe_snake_case

if TYPE_CHECKING:
from grpclib._protocols import IProtoMessage
from grpclib.client import Channel
from grpclib.metadata import Deadline

if not (sys.version_info.major == 3 and sys.version_info.minor >= 7):
# Apply backport of datetime.fromisoformat from 3.7
Expand Down Expand Up @@ -1013,10 +1015,10 @@ class ServiceStub(ABC):

def __init__(
self,
channel: grpclib.client.Channel,
channel: "Channel",
*,
timeout: Optional[float] = None,
deadline: Optional[grpclib.metadata.Deadline] = None,
deadline: Optional["Deadline"] = None,
metadata: Optional[_MetadataLike] = None,
) -> None:
self.channel = channel
Expand All @@ -1027,7 +1029,7 @@ def __init__(
def __resolve_request_kwargs(
self,
timeout: Optional[float],
deadline: Optional[grpclib.metadata.Deadline],
deadline: Optional["Deadline"],
metadata: Optional[_MetadataLike],
):
return {
Expand All @@ -1043,7 +1045,7 @@ async def _unary_unary(
response_type: Type[T],
*,
timeout: Optional[float] = None,
deadline: Optional[grpclib.metadata.Deadline] = None,
deadline: Optional["Deadline"] = None,
metadata: Optional[_MetadataLike] = None,
) -> T:
"""Make a unary request and return the response."""
Expand All @@ -1066,7 +1068,7 @@ async def _unary_stream(
response_type: Type[T],
*,
timeout: Optional[float] = None,
deadline: Optional[grpclib.metadata.Deadline] = None,
deadline: Optional["Deadline"] = None,
metadata: Optional[_MetadataLike] = None,
) -> AsyncGenerator[T, None]:
"""Make a unary request and return the stream response iterator."""
Expand Down
2 changes: 1 addition & 1 deletion betterproto/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def get_py_zero(type_num: int) -> str:


def traverse(proto_file):
def _traverse(path, items, prefix = ''):
def _traverse(path, items, prefix=""):
for i, item in enumerate(items):
# Adjust the name since we flatten the heirarchy.
item.name = next_prefix = prefix + item.name
Expand Down
2 changes: 1 addition & 1 deletion betterproto/tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class TestParentMessage(betterproto.Message):
some_double: float = betterproto.double_field(2)
some_message: TestChildMessage = betterproto.message_field(3)

test = TestParentMessage().from_dict({"someInt": 0, "someDouble": 1.2,})
test = TestParentMessage().from_dict({"someInt": 0, "someDouble": 1.2})

assert test.to_dict(include_default_values=True) == {
"someInt": 0,
Expand Down
10 changes: 7 additions & 3 deletions betterproto/tests/test_service_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
grpclib.const.Cardinality.UNARY_UNARY,
DoThingRequest,
DoThingResponse,
),
)
}


Expand Down Expand Up @@ -94,7 +94,9 @@ async def test_service_call_lower_level_with_overrides():
) as channel:
stub = ExampleServiceStub(channel, deadline=deadline, metadata=metadata)
response = await stub._unary_unary(
"/service.ExampleService/DoThing", DoThingRequest(ITERATIONS), DoThingResponse,
"/service.ExampleService/DoThing",
DoThingRequest(ITERATIONS),
DoThingResponse,
deadline=kwarg_deadline,
metadata=kwarg_metadata,
)
Expand All @@ -116,7 +118,9 @@ async def test_service_call_lower_level_with_overrides():
) as channel:
stub = ExampleServiceStub(channel, deadline=deadline, metadata=metadata)
response = await stub._unary_unary(
"/service.ExampleService/DoThing", DoThingRequest(ITERATIONS), DoThingResponse,
"/service.ExampleService/DoThing",
DoThingRequest(ITERATIONS),
DoThingResponse,
timeout=kwarg_timeout,
metadata=kwarg_metadata,
)
Expand Down