Skip to content

Commit fa54e6f

Browse files
committed
Fix most mypy warnings
1 parent 710fc4a commit fa54e6f

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

betterproto/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class ProtoClassMetadata:
440440

441441
def __init__(self, cls: Type["Message"]):
442442
by_field = {}
443-
by_group = {}
443+
by_group: Dict[str, Set] = {}
444444
by_field_name = {}
445445
by_field_number = {}
446446

@@ -780,7 +780,7 @@ def FromString(cls: Type[T], data: bytes) -> T:
780780

781781
def to_dict(
782782
self, casing: Casing = Casing.CAMEL, include_default_values: bool = False
783-
) -> dict:
783+
) -> Dict[str, Any]:
784784
"""
785785
Returns a dict representation of this message instance which can be
786786
used to serialize to e.g. JSON. Defaults to camel casing for

betterproto/_types.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from typing import TypeVar
1+
from typing import TYPE_CHECKING, TypeVar
2+
3+
if TYPE_CHECKING:
4+
from . import Message
5+
from grpclib._protocols import IProtoMessage
26

37
# Bound type variable to allow methods to return `self` of subclasses
48
T = TypeVar("T", bound="Message")

betterproto/grpc/grpclib_client.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import grpclib.const
44
from typing import (
55
Any,
6+
AsyncIterable,
67
AsyncIterator,
78
Collection,
8-
Iterator,
9+
Iterable,
910
Mapping,
1011
Optional,
1112
Tuple,
@@ -23,7 +24,7 @@
2324

2425
_Value = Union[str, bytes]
2526
_MetadataLike = Union[Mapping[str, _Value], Collection[Tuple[str, _Value]]]
26-
_MessageSource = Union[Iterator["IProtoMessage"], AsyncIterator["IProtoMessage"]]
27+
_MessageSource = Union[Iterable["IProtoMessage"], AsyncIterable["IProtoMessage"]]
2728

2829

2930
class ServiceStub(ABC):
@@ -158,7 +159,7 @@ async def _stream_stream(
158159

159160
@staticmethod
160161
async def _send_messages(stream, messages: _MessageSource):
161-
if hasattr(messages, "__aiter__"):
162+
if isinstance(messages, AsyncIterable):
162163
async for message in messages:
163164
await stream.send_message(message)
164165
else:

betterproto/grpc/util/async_channel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async def send_from(
5353
"""
5454
if self._closed:
5555
raise self.Error("Cannot send through a closed channel")
56-
if hasattr(source, "__aiter__"):
56+
if isinstance(source, AsyncIterable):
5757
async for item in source:
5858
await self._queue.put(item)
5959
else:

betterproto/plugin.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import stringcase
77
import sys
88
import textwrap
9-
from typing import Dict, List, Optional, Type
9+
from typing import Dict, List, Optional, Type, Union
1010
from betterproto.casing import safe_snake_case
1111

1212
try:
@@ -120,8 +120,8 @@ def py_type(
120120
raise NotImplementedError(f"Unknown type {descriptor.type}")
121121

122122

123-
def get_py_zero(type_num: int) -> str:
124-
zero = 0
123+
def get_py_zero(type_num: int) -> Union[str, float]:
124+
zero: Union[str, float] = 0
125125
if type_num in []:
126126
zero = 0.0
127127
elif type_num == 8:

0 commit comments

Comments
 (0)