Skip to content

Commit 0fd572b

Browse files
committed
fix(translation): make _bind_to_map pure and prevent double-wrapping
- Add copy.deepcopy to avoid mutating input params (side effect bug) - Check for existing map_flag to prevent double-wrapping on repeated calls - Handle edge cases for non-list/non-dict types (primitives, None) - Import copy module at top of file Fixes side effects bug where calling _bind_to_map multiple times would mutate the original params and potentially double-wrap the map_flag. Entire-Checkpoint: a0dbc5306a76
1 parent f50afef commit 0fd572b

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

roborock/map/translation.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from __future__ import annotations
1111

1212
import asyncio
13+
import copy
1314
import logging
1415
from abc import ABC, abstractmethod
1516
from dataclasses import dataclass
@@ -454,15 +455,28 @@ def _bind_to_map(self, params: Any, map_flag: int) -> Any:
454455
"""Bind command parameters to a specific map flag.
455456
456457
This ensures commands apply to the correct floor in multi-map setups.
458+
459+
This is a pure function - it does not mutate the input params.
457460
"""
461+
# Create a deep copy to avoid mutating the original input
462+
params = copy.deepcopy(params)
463+
458464
# Different protocols handle map binding differently
459465
if isinstance(params, dict):
460-
params["map_flag"] = map_flag
466+
# Prevent double-wrapping by checking if already bound
467+
if params.get("map_flag") != map_flag:
468+
params["map_flag"] = map_flag
461469
elif isinstance(params, list):
462-
# For list params, we may need to wrap in a dict or prepend
470+
# For list params, prepend the map_flag
463471
# Prevent double-wrapping if map_flag is already prepended
464472
if not params or params[0] != map_flag:
465473
params = [map_flag] + params
474+
# For other types (primitives, None, etc.), return as-is with map_flag wrapped in a list
475+
elif params is not None:
476+
params = [map_flag, params]
477+
else:
478+
params = [map_flag]
479+
466480
return params
467481

468482
async def create_map_backup(self, map_flag: int | None = None) -> bool:

0 commit comments

Comments
 (0)