From a517594722f6c6cfbed349e850b8ebdd043718ec Mon Sep 17 00:00:00 2001 From: Martino Russi Date: Fri, 24 Jul 2026 12:08:15 +0200 Subject: [PATCH] style: fix ruff SIM105/format lints in unitree_g1 scripts Use contextlib.suppress(zmq.Again) instead of try/except/pass in the onboard state publisher and teleop client, plus ruff-format/isort autofixes across the unitree_g1 helpers and dataset_reader imports. Co-authored-by: Cursor --- src/lerobot/cameras/zmq/image_server.py | 5 ++++- src/lerobot/datasets/dataset_reader.py | 2 +- .../robots/unitree_g1/run_g1_onboard.py | 9 ++++---- .../robots/unitree_g1/run_g1_server.py | 6 +----- .../robots/unitree_g1/run_g1_teleop_client.py | 5 ++--- src/lerobot/robots/unitree_g1/unitree_g1.py | 21 +++++++++++++++---- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/lerobot/cameras/zmq/image_server.py b/src/lerobot/cameras/zmq/image_server.py index 7b0efe579..3f846cee5 100644 --- a/src/lerobot/cameras/zmq/image_server.py +++ b/src/lerobot/cameras/zmq/image_server.py @@ -137,7 +137,10 @@ class ImageServer: last_err = e logger.warning( "Camera %s open attempt %d/%d failed: %s", - name, attempt, self.open_attempts, e, + name, + attempt, + self.open_attempts, + e, ) with contextlib.suppress(Exception): camera.disconnect() diff --git a/src/lerobot/datasets/dataset_reader.py b/src/lerobot/datasets/dataset_reader.py index f4e1f6a31..445b309f2 100644 --- a/src/lerobot/datasets/dataset_reader.py +++ b/src/lerobot/datasets/dataset_reader.py @@ -19,9 +19,9 @@ from collections.abc import Callable from concurrent.futures import ThreadPoolExecutor from pathlib import Path -import datasets import torch +import datasets from lerobot.configs import ( DEFAULT_DEPTH_UNIT, DEPTH_METER_UNIT, diff --git a/src/lerobot/robots/unitree_g1/run_g1_onboard.py b/src/lerobot/robots/unitree_g1/run_g1_onboard.py index b3aa4d60f..e76457fa1 100644 --- a/src/lerobot/robots/unitree_g1/run_g1_onboard.py +++ b/src/lerobot/robots/unitree_g1/run_g1_onboard.py @@ -44,6 +44,7 @@ Examples (on the robot): """ import argparse +import contextlib import json import logging import os @@ -177,8 +178,8 @@ def main() -> None: logger.warning("E-STOP ('e'): going passive NOW.") try: robot._shutdown_event.set() # stop the 50Hz controller loop publishing - time.sleep(0.05) # let it finish its current cycle - robot._send_zero_torque() # motors limp; nothing overwrites it now + time.sleep(0.05) # let it finish its current cycle + robot._send_zero_torque() # motors limp; nothing overwrites it now except Exception as e: # noqa: BLE001 logger.warning("E-stop zero-torque failed: %s", e) os._exit(0) # immediate hard exit, no slow cleanup @@ -218,10 +219,8 @@ def main() -> None: state["groot.rpy.roll"] = float(orient[0]) state["groot.rpy.pitch"] = float(orient[1]) state["groot.rpy.yaw"] = float(orient[2]) - try: + with contextlib.suppress(zmq.Again): state_sock.send_json(state, zmq.NOBLOCK) - except zmq.Again: - pass time.sleep(max(0.0, period - (time.time() - t0))) threading.Thread(target=publish_state, daemon=True).start() diff --git a/src/lerobot/robots/unitree_g1/run_g1_server.py b/src/lerobot/robots/unitree_g1/run_g1_server.py index 0dd400b8b..862bc4f1c 100644 --- a/src/lerobot/robots/unitree_g1/run_g1_server.py +++ b/src/lerobot/robots/unitree_g1/run_g1_server.py @@ -151,11 +151,7 @@ def parse_camera_specs(spec: str, default_width: int, default_height: int) -> di # a pixel format. Real device-path tail segments (e.g. "1.0-video-index0") # won't match these strict patterns. fourcc = None - if ( - len(tokens) >= 3 - and wh_re.fullmatch(tokens[-2]) - and fourcc_re.fullmatch(tokens[-1]) - ): + if len(tokens) >= 3 and wh_re.fullmatch(tokens[-2]) and fourcc_re.fullmatch(tokens[-1]): fourcc = tokens.pop().upper() width, height = default_width, default_height if len(tokens) >= 2 and wh_re.fullmatch(tokens[-1]): diff --git a/src/lerobot/robots/unitree_g1/run_g1_teleop_client.py b/src/lerobot/robots/unitree_g1/run_g1_teleop_client.py index 46f494ffb..c5afe0be3 100644 --- a/src/lerobot/robots/unitree_g1/run_g1_teleop_client.py +++ b/src/lerobot/robots/unitree_g1/run_g1_teleop_client.py @@ -32,6 +32,7 @@ Example (on the laptop): """ import argparse +import contextlib import logging import time @@ -106,10 +107,8 @@ def main() -> None: rc.lx = rc.ly = rc.rx = rc.ry = 0.0 rc.button = [0] * 16 action = teleop.get_action() - try: + with contextlib.suppress(zmq.Again): sock.send_json(_to_jsonable(action), zmq.NOBLOCK) - except zmq.Again: - pass n += 1 if n % 60 == 0: axes = { diff --git a/src/lerobot/robots/unitree_g1/unitree_g1.py b/src/lerobot/robots/unitree_g1/unitree_g1.py index ad37c7298..e5b31c73c 100644 --- a/src/lerobot/robots/unitree_g1/unitree_g1.py +++ b/src/lerobot/robots/unitree_g1/unitree_g1.py @@ -70,8 +70,22 @@ else: # teleoperator's RemoteController so the onboard path reads the physical Unitree remote # identically. _REMOTE_BUTTON_MAP: list[str] = [ - "RB", "LB", "start", "back", "RT", "LT", "", "", - "A", "B", "X", "Y", "up", "right", "down", "left", + "RB", + "LB", + "start", + "back", + "RT", + "LT", + "", + "", + "A", + "B", + "X", + "Y", + "up", + "right", + "down", + "left", ] logger = logging.getLogger(__name__) @@ -299,8 +313,7 @@ class UnitreeG1(Robot): actual_hz = loop_count / (time.time() - last_log_time) eff = {k: round(float(controller_input.get(k, 0.0)), 3) for k in REMOTE_AXES} logger.info( - f"Controller {actual_hz:.1f}Hz | eff_axes={eff} " - f"wireless={'ACTIVE' if wl else 'idle'}" + f"Controller {actual_hz:.1f}Hz | eff_axes={eff} wireless={'ACTIVE' if wl else 'idle'}" ) loop_count = 0 last_log_time = time.time()