8000 Typing fixes · enixdark/python-betterproto@1668721 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1668721

Browse files
committed
Typing fixes
1 parent eb5020d commit 1668721

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

betterproto/__init__.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,14 @@ class Message(ABC):
420420
register the message fields which get used by the serializers and parsers
421421
to go between Python, binary and JSON protobuf message representations.
422422
"""
423+
_serialized_on_wire: bool
424+
_unknown_fields: bytes
425+
_group_map: Dict[str, dict]
423426

424427
def __post_init__(self) -> None:
425428
# Set a default value for each field in the class after `__init__` has
426429
# already been run.
427-
group_map = {"fields": {}, "groups": {}}
430+
group_map: Dict[str, dict] = {"fields": {}, "groups": {}}
428431
for field in dataclasses.fields(self):
429432
meta = FieldMetadata.get(field)
430433

@@ -518,7 +521,7 @@ def __bytes__(self) -> bytes:
518521
else:
519522
for item in value:
520523
output += _serialize_single(
521-
meta.number, meta.proto_type, item, wraps=meta.wraps
524+
meta.number, meta.proto_type, item, wraps=meta.wraps or ""
522525
)
523526
elif isinstance(value, dict):
524527
for k, v in value.items():
@@ -532,7 +535,7 @@ def __bytes__(self) -> bytes:
532535
meta.proto_type,
533536
value,
534537
serialize_empty=serialize_empty,
535-
wraps=meta.wraps,
538+
wraps=meta.wraps or "",
536539
)
537540

538541
return output + self._unknown_fields
@@ -702,14 +705,14 @@ def to_dict(self, casing: Casing = Casing.CAMEL) -> dict:
702705
for field in dataclasses.fields(self):
703706
meta = FieldMetadata.get(field)
704707
v = getattr(self, field.name)
705-
cased_name = casing(field.name).rstrip("_")
708+
cased_name = casing(field.name).rstrip("_") # type: ignore
706709
if meta.proto_type == "message":
707710
if isinstance(v, datetime):
708711
if v != DATETIME_ZERO:
709-
output[cased_name] = _Timestamp.to_json(v)
712+
output[cased_name] = _Timestamp.timestamp_to_json(v)
710713
elif isinstance(v, timedelta):
711714
if v != timedelta(0):
712-
output[cased_name] = _Duration.to_json(v)
715+
output[cased_name] = _Duration.delta_to_json(v)
713716
elif meta.wraps:
714717
if v is not None:
715718
output[cased_name] = v
@@ -738,7 +741,7 @@ def to_dict(self, casing: Casing = Casing.CAMEL) -> dict:
738741
else:
739742
output[cased_name] = b64encode(v).decode("utf8")
740743
elif meta.proto_type == TYPE_ENUM:
741-
enum_values = list(self._cls_for(field))
744+
enum_values = list(self._cls_for(field)) # type: ignore
742745
if isinstance(v, list):
743746
output[cased_name] = [enum_values[e].name for e in v]
744747
else:
@@ -853,7 +856,7 @@ def to_timedelta(self) -> timedelta:
853856
return timedelta(seconds=self.seconds, microseconds=self.nanos / 1e3)
854857

855858
@staticmethod
856-
def to_json(delta: timedelta) -> str:
859+
def delta_to_json(delta: timedelta) -> str:
857860
parts = str(delta.total_seconds()).split(".")
858861
if len(parts) > 1:
859862
while len(parts[1]) not in [3, 6, 9]:
@@ -876,7 +879,7 @@ def to_datetime(self) -> datetime:
876879
return datetime.fromtimestamp(ts, tz=timezone.utc)
877880

878881
@staticmethod
879-
def to_json(dt: datetime) -> str:
882+
def timestamp_to_json(dt: datetime) -> str:
880883
nanos = dt.microsecond * 1e3
881884
copy = dt.replace(microsecond=0, tzinfo=None)
882885
result = copy.isoformat()
@@ -899,12 +902,15 @@ class _WrappedMessage(Message):
899902
Google protobuf wrapper types base class. JSON representation is just the
900903
value itself.
901904
"""
902-
def to_dict(self) -> Any:
905+
value: Any
906+
907+
def to_dict(self, casing: Casing = Casing.CAMEL) -> Any:
903908
return self.value
904909

905-
def from_dict(self, value: Any) -> None:
910+
def from_dict(self: T, value: Any) -> T:
906911
if value is not None:
907912
self.value = value
913+
return self
908914

909915

910916
@dataclasses.dataclass
@@ -952,7 +958,7 @@ class _BytesValue(_WrappedMessage):
952958
value: bytes = bytes_field(1)
953959

954960

955-
def _get_wrapper(proto_type: str) -> _WrappedMessage:
961+
def _get_wrapper(proto_type: str) -> Type:
956962
"""Get the wrapper message class for a wrapped type."""
957963
return {
958964
TYPE_BOOL: _BoolValue,

0 commit comments

Comments
 (0)
0