From c8229e53a7b73f60c4d658e512cebb1cf8080a7d Mon Sep 17 00:00:00 2001 From: Nat Noordanus Date: Sun, 7 Jun 2020 19:10:41 +0200 Subject: [PATCH] Fix most mypy warnings --- betterproto/__init__.py | 4 ++-- betterproto/_types.py | 6 +++++- betterproto/grpc/grpclib_client.py | 7 ++++--- betterproto/plugin.py | 8 ++++---- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/betterproto/__init__.py b/betterproto/__init__.py index 6a53d65..c1e60ea 100644 --- a/betterproto/__init__.py +++ b/betterproto/__init__.py @@ -440,7 +440,7 @@ class ProtoClassMetadata: def __init__(self, cls: Type["Message"]): by_field = {} - by_group = {} + by_group: Dict[str, Set] = {} by_field_name = {} by_field_number = {} @@ -780,7 +780,7 @@ class Message(ABC): def to_dict( self, casing: Casing = Casing.CAMEL, include_default_values: bool = False - ) -> dict: + ) -> Dict[str, Any]: """ Returns a dict representation of this message instance which can be used to serialize to e.g. JSON. Defaults to camel casing for diff --git a/betterproto/_types.py b/betterproto/_types.py index 0ff23e4..d03432c 100644 --- a/betterproto/_types.py +++ b/betterproto/_types.py @@ -1,4 +1,8 @@ -from typing import TypeVar +from typing import TYPE_CHECKING, TypeVar + +if TYPE_CHECKING: + from . import Message + from grpclib._protocols import IProtoMessage # Bound type variable to allow methods to return `self` of subclasses T = TypeVar("T", bound="Message") diff --git a/betterproto/grpc/grpclib_client.py b/betterproto/grpc/grpclib_client.py index 7218574..7f48fb9 100644 --- a/betterproto/grpc/grpclib_client.py +++ b/betterproto/grpc/grpclib_client.py @@ -3,9 +3,10 @@ import asyncio import grpclib.const from typing import ( Any, + AsyncIterable, AsyncIterator, Collection, - Iterator, + Iterable, Mapping, Optional, Tuple, @@ -23,7 +24,7 @@ if TYPE_CHECKING: _Value = Union[str, bytes] _MetadataLike = Union[Mapping[str, _Value], Collection[Tuple[str, _Value]]] -_MessageSource = Union[Iterator["IProtoMessage"], AsyncIterator["IProtoMessage"]] +_MessageSource = Union[Iterable["IProtoMessage"], AsyncIterable["IProtoMessage"]] class ServiceStub(ABC): @@ -160,7 +161,7 @@ class ServiceStub(ABC): @staticmethod async def _send_messages(stream, messages: _MessageSource): - if hasattr(messages, "__aiter__"): + if isinstance(messages, AsyncIterable): async for message in messages: await stream.send_message(message) else: diff --git a/betterproto/plugin.py b/betterproto/plugin.py index 85fd905..ed14e00 100755 --- a/betterproto/plugin.py +++ b/betterproto/plugin.py @@ -6,10 +6,10 @@ import re import stringcase import sys import textwrap -from typing import List +from typing import List, Union +import betterproto from betterproto.casing import safe_snake_case from betterproto.compile.importing import get_ref_type -import betterproto try: # betterproto[compiler] specific dependencies @@ -58,8 +58,8 @@ def py_type( raise NotImplementedError(f"Unknown type {descriptor.type}") -def get_py_zero(type_num: int) -> str: - zero = 0 +def get_py_zero(type_num: int) -> Union[str, float]: + zero: Union[str, float] = 0 if type_num in []: zero = 0.0 elif type_num == 8: