Skip to content

Commit

Permalink
mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
InvincibleRMC committed Mar 28, 2024
1 parent e6008aa commit 5f905b2
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions mqtt_ros_bridge/encodings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Protocol, Type, TypeVar, NoReturn, TypeAlias
from typing import Protocol, Type, TypeVar, TypeAlias

import json
from rclpy.type_support import check_is_valid_msg_type


NestedDictionary: TypeAlias = dict[str, 'NestedDictionary'] | dict[str, str] | dict[str, NoReturn]
NestedDictionary: TypeAlias = dict[str, 'NestedDictionary'] | dict[str, str]


class MsgLike(Protocol):
Expand All @@ -27,7 +27,7 @@ def human_readable_encoding(msg: MsgLike) -> bytes:


def human_readable_encoding_recursive(msg: MsgLike) -> NestedDictionary:
msg_dict: NestedDictionary = {}
msg_dict = {}
for field, field_types in (msg.get_fields_and_field_types()).items():
value = getattr(msg, field)

Expand All @@ -53,13 +53,15 @@ def human_readable_decoding(byte_msg: bytes, msg_type: Type[MsgLikeT]) -> MsgLik
def human_readable_decoding_recursive(msg_dict: NestedDictionary,
msg_type: Type[MsgLikeT]) -> MsgLikeT:
msg = msg_type()

set_value: object
for field, value in msg_dict.items():
if isinstance(getattr(msg, field), bytes):
if isinstance(value, str):
value = value.encode()
set_value = value.encode()
elif isinstance(value, dict):
value = human_readable_decoding_recursive(value, type(getattr(msg, field)))
set_value = human_readable_decoding_recursive(value, type(getattr(msg, field)))
else:
set_value = value

setattr(msg, field, value)
setattr(msg, field, set_value)
return msg

0 comments on commit 5f905b2

Please sign in to comment.