Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions roborock/data/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def _convert_to_class_obj(class_type: type, value):
sub_type = get_args(class_type)[0]
return [RoborockBase._convert_to_class_obj(sub_type, obj) for obj in value]
if get_origin(class_type) is dict:
_, value_type = get_args(class_type) # assume keys are only basic types
return {k: RoborockBase._convert_to_class_obj(value_type, v) for k, v in value.items()}
key_type, value_type = get_args(class_type)
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unpacking will fail with a ValueError: not enough values to unpack when encountering untyped dict fields (e.g., device_status: dict | None at line 242 in this file). For untyped dicts, get_args(dict) returns an empty tuple ().

Consider adding a check for this case:

args = get_args(class_type)
if not args:
    # Untyped dict - no conversion needed
    return value
key_type, value_type = args
Suggested change
key_type, value_type = get_args(class_type)
args = get_args(class_type)
if not args:
# Untyped dict - no conversion needed
return value
key_type, value_type = args

Copilot uses AI. Check for mistakes.
return {key_type(k): RoborockBase._convert_to_class_obj(value_type, v) for k, v in value.items()}
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new key type conversion behavior lacks test coverage. Consider adding a test case in tests/test_containers.py that verifies dictionary keys are correctly converted when deserializing from JSON. For example:

@dataclass
class ObjectWithIntKeys(RoborockBase):
    data: dict[int, str] | None = None

def test_dict_int_key_conversion():
    """Test that int keys are preserved when round-tripping through JSON."""
    obj = ObjectWithIntKeys(data={1: "one", 2: "two"})
    json_data = json.dumps(obj.as_dict())
    restored = ObjectWithIntKeys.from_dict(json.loads(json_data))
    assert restored.data == {1: "one", 2: "two"}
    assert all(isinstance(k, int) for k in restored.data.keys())

This would help prevent regressions of the duplicate key issue mentioned in the PR description.

Copilot uses AI. Check for mistakes.
if inspect.isclass(class_type):
if issubclass(class_type, RoborockBase):
return class_type.from_dict(value)
Expand Down