Skip to content

Commit c3ae98b

Browse files
fix: Fix operator precedence bug with walrus operator in cli.py command execution (#822)
## Description There is a critical operator precedence bug in the `command` function inside `roborock/cli.py` affecting `B01_Q10` devices (and potentially others using the same logic). Currently, the code reads: `if cmd_value := B01_Q10_DP.from_any_optional(cmd) is None:` Because the `is` operator binds tighter than the walrus operator (`:=`), Python evaluates `(B01_Q10_DP.from_any_optional(cmd) is None)` first. If a valid command is passed, this evaluates to `False`, and the boolean `False` is assigned to `cmd_value`. The `if` block is then skipped, and `cmd_value` (`False`) is passed to `command_trait.send()`. This results in a crash shortly after: `AttributeError: 'bool' object has no attribute 'code'` ## Fix Adding parentheses around the assignment expression fixes the evaluation order: `if (cmd_value := B01_Q10_DP.from_any_optional(cmd)) is None:`
1 parent 4ebb214 commit c3ae98b

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

roborock/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ async def command(ctx, cmd, device_id, params):
780780
if result:
781781
click.echo(dump_json(result))
782782
elif device.b01_q10_properties is not None:
783-
if cmd_value := B01_Q10_DP.from_any_optional(cmd) is None:
783+
if (cmd_value := B01_Q10_DP.from_any_optional(cmd)) is None:
784784
raise RoborockException(f"Invalid command {cmd} for B01_Q10 device")
785785
command_trait: Trait = device.b01_q10_properties.command
786786
await command_trait.send(cmd_value, json.loads(params) if params is not None else None)

0 commit comments

Comments
 (0)