Fix most mypy warnings

This commit is contained in:
Nat Noordanus 2020-06-07 19:10:41 +02:00
parent 3185c67098
commit c8229e53a7
4 changed files with 15 additions and 10 deletions

View File

@ -440,7 +440,7 @@ class ProtoClassMetadata:
def __init__(self, cls: Type["Message"]): def __init__(self, cls: Type["Message"]):
by_field = {} by_field = {}
by_group = {} by_group: Dict[str, Set] = {}
by_field_name = {} by_field_name = {}
by_field_number = {} by_field_number = {}
@ -780,7 +780,7 @@ class Message(ABC):
def to_dict( def to_dict(
self, casing: Casing = Casing.CAMEL, include_default_values: bool = False 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 Returns a dict representation of this message instance which can be
used to serialize to e.g. JSON. Defaults to camel casing for used to serialize to e.g. JSON. Defaults to camel casing for

View File

@ -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 # Bound type variable to allow methods to return `self` of subclasses
T = TypeVar("T", bound="Message") T = TypeVar("T", bound="Message")

View File

@ -3,9 +3,10 @@ import asyncio
import grpclib.const import grpclib.const
from typing import ( from typing import (
Any, Any,
AsyncIterable,
AsyncIterator, AsyncIterator,
Collection, Collection,
Iterator, Iterable,
Mapping, Mapping,
Optional, Optional,
Tuple, Tuple,
@ -23,7 +24,7 @@ if TYPE_CHECKING:
_Value = Union[str, bytes] _Value = Union[str, bytes]
_MetadataLike = Union[Mapping[str, _Value], Collection[Tuple[str, _Value]]] _MetadataLike = Union[Mapping[str, _Value], Collection[Tuple[str, _Value]]]
_MessageSource = Union[Iterator["IProtoMessage"], AsyncIterator["IProtoMessage"]] _MessageSource = Union[Iterable["IProtoMessage"], AsyncIterable["IProtoMessage"]]
class ServiceStub(ABC): class ServiceStub(ABC):
@ -160,7 +161,7 @@ class ServiceStub(ABC):
@staticmethod @staticmethod
async def _send_messages(stream, messages: _MessageSource): async def _send_messages(stream, messages: _MessageSource):
if hasattr(messages, "__aiter__"): if isinstance(messages, AsyncIterable):
async for message in messages: async for message in messages:
await stream.send_message(message) await stream.send_message(message)
else: else:

View File

@ -6,10 +6,10 @@ import re
import stringcase import stringcase
import sys import sys
import textwrap import textwrap
from typing import List from typing import List, Union
import betterproto
from betterproto.casing import safe_snake_case from betterproto.casing import safe_snake_case
from betterproto.compile.importing import get_ref_type from betterproto.compile.importing import get_ref_type
import betterproto
try: try:
# betterproto[compiler] specific dependencies # betterproto[compiler] specific dependencies
@ -58,8 +58,8 @@ def py_type(
raise NotImplementedError(f"Unknown type {descriptor.type}") raise NotImplementedError(f"Unknown type {descriptor.type}")
def get_py_zero(type_num: int) -> str: def get_py_zero(type_num: int) -> Union[str, float]:
zero = 0 zero: Union[str, float] = 0
if type_num in []: if type_num in []:
zero = 0.0 zero = 0.0
elif type_num == 8: elif type_num == 8: