From 27122bbd281395fc74c796868532a8f0359aaeaa Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Sat, 25 Jul 2026 10:29:37 -0700 Subject: [PATCH] lint: check indentation (#38454) * lint: check indentation * cleanup * happy * that was too much * happy --- .../selfdrive/assets/sounds/make_beeps.py | 10 ++-- openpilot/selfdrive/locationd/helpers.py | 46 +++++++++---------- .../selfdrive/ui/mici/onroad/cameraview.py | 12 ++--- openpilot/system/athena/athenad.py | 2 +- openpilot/system/athena/tests/test_athenad.py | 2 +- openpilot/tools/lib/framereader.py | 2 +- .../sim/bridge/metadrive/metadrive_process.py | 4 +- scripts/lint/check_indentation.py | 38 +++++++++++++++ scripts/lint/lint.sh | 2 + 9 files changed, 79 insertions(+), 39 deletions(-) create mode 100755 scripts/lint/check_indentation.py diff --git a/openpilot/selfdrive/assets/sounds/make_beeps.py b/openpilot/selfdrive/assets/sounds/make_beeps.py index 6161e80e74..e4c10c8d19 100644 --- a/openpilot/selfdrive/assets/sounds/make_beeps.py +++ b/openpilot/selfdrive/assets/sounds/make_beeps.py @@ -6,12 +6,12 @@ sr = 48000 max_int16 = 2**15 - 1 def harmonic_beep(freq, duration_seconds): - n_total = int(sr * duration_seconds) + n_total = int(sr * duration_seconds) - signal = np.sin(2 * np.pi * freq * np.arange(n_total) / sr) - x = np.arange(n_total) - exp_scale = np.exp(-x/5.5e3) - return max_int16 * signal * exp_scale + signal = np.sin(2 * np.pi * freq * np.arange(n_total) / sr) + x = np.arange(n_total) + exp_scale = np.exp(-x/5.5e3) + return max_int16 * signal * exp_scale engage_beep = harmonic_beep(1661.219, 0.5) wavfile.write("engage.wav", sr, engage_beep.astype(np.int16)) diff --git a/openpilot/selfdrive/locationd/helpers.py b/openpilot/selfdrive/locationd/helpers.py index 5e3fa16027..9ea9b6bf4f 100644 --- a/openpilot/selfdrive/locationd/helpers.py +++ b/openpilot/selfdrive/locationd/helpers.py @@ -9,29 +9,29 @@ from openpilot.common.transformations.orientation import rot_from_euler, euler_f @cache def fft_next_good_size(n: int) -> int: - """ - smallest composite of 2, 3, 5, 7, 11 that is >= n - inspired by pocketfft - """ - if n <= 6: - return n - best, f2 = 2 * n, 1 - while f2 < best: - f23 = f2 - while f23 < best: - f235 = f23 - while f235 < best: - f2357 = f235 - while f2357 < best: - f235711 = f2357 - while f235711 < best: - best = f235711 if f235711 >= n else best - f235711 *= 11 - f2357 *= 7 - f235 *= 5 - f23 *= 3 - f2 *= 2 - return best + """ + smallest composite of 2, 3, 5, 7, 11 that is >= n + inspired by pocketfft + """ + if n <= 6: + return n + best, f2 = 2 * n, 1 + while f2 < best: + f23 = f2 + while f23 < best: + f235 = f23 + while f235 < best: + f2357 = f235 + while f2357 < best: + f235711 = f2357 + while f235711 < best: + best = f235711 if f235711 >= n else best + f235711 *= 11 + f2357 *= 7 + f235 *= 5 + f23 *= 3 + f2 *= 2 + return best def parabolic_peak_interp(R, max_index): diff --git a/openpilot/selfdrive/ui/mici/onroad/cameraview.py b/openpilot/selfdrive/ui/mici/onroad/cameraview.py index 82e4865c76..4a31a9e04e 100644 --- a/openpilot/selfdrive/ui/mici/onroad/cameraview.py +++ b/openpilot/selfdrive/ui/mici/onroad/cameraview.py @@ -383,12 +383,12 @@ class CameraView(Widget): self._initialize_textures() def _initialize_textures(self): - self._clear_textures() - if not TICI: - self.texture_y = rl.load_texture_from_image(rl.Image(None, int(self.client.stride), - int(self.client.height), 1, rl.PixelFormat.PIXELFORMAT_UNCOMPRESSED_GRAYSCALE)) - self.texture_uv = rl.load_texture_from_image(rl.Image(None, int(self.client.stride // 2), - int(self.client.height // 2), 1, rl.PixelFormat.PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA)) + self._clear_textures() + if not TICI: + self.texture_y = rl.load_texture_from_image(rl.Image(None, int(self.client.stride), + int(self.client.height), 1, rl.PixelFormat.PIXELFORMAT_UNCOMPRESSED_GRAYSCALE)) + self.texture_uv = rl.load_texture_from_image(rl.Image(None, int(self.client.stride // 2), + int(self.client.height // 2), 1, rl.PixelFormat.PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA)) def _clear_textures(self): if self.texture_y and self.texture_y.id: diff --git a/openpilot/system/athena/athenad.py b/openpilot/system/athena/athenad.py index 91b12fc0a7..e7c318499a 100755 --- a/openpilot/system/athena/athenad.py +++ b/openpilot/system/athena/athenad.py @@ -580,7 +580,7 @@ def startStream(sdp: str, enabled: bool) -> dict: if CP.notCar: bridge_services_in.append("testJoystick") else: - raise Exception("failed to get CarParamsPersistent") + raise Exception("failed to get CarParamsPersistent") if params.get_bool("IsOffroad"): # manager owns camerad/stream_encoderd/webrtcd; flip the param and let it bring them up. diff --git a/openpilot/system/athena/tests/test_athenad.py b/openpilot/system/athena/tests/test_athenad.py index dbc1050db8..c4cd93558c 100644 --- a/openpilot/system/athena/tests/test_athenad.py +++ b/openpilot/system/athena/tests/test_athenad.py @@ -49,7 +49,7 @@ def with_upload_handler(func): return wrapper def mock_create_connection(mocker): - return mocker.patch('openpilot.system.athena.athenad.create_connection') + return mocker.patch('openpilot.system.athena.athenad.create_connection') def host(): with http_server_context(handler=HTTPRequestHandler, setup=seed_athena_server) as (host, port): diff --git a/openpilot/tools/lib/framereader.py b/openpilot/tools/lib/framereader.py index c923651563..62c75b95f7 100644 --- a/openpilot/tools/lib/framereader.py +++ b/openpilot/tools/lib/framereader.py @@ -28,7 +28,7 @@ class LRUCache: def __setitem__(self, key, value): self._cache[key] = value if len(self._cache) > self.capacity: - self._cache.popitem(last=False) + self._cache.popitem(last=False) def __contains__(self, key): return key in self._cache diff --git a/openpilot/tools/sim/bridge/metadrive/metadrive_process.py b/openpilot/tools/sim/bridge/metadrive/metadrive_process.py index 2486d87ff9..01d0473755 100644 --- a/openpilot/tools/sim/bridge/metadrive/metadrive_process.py +++ b/openpilot/tools/sim/bridge/metadrive/metadrive_process.py @@ -27,9 +27,9 @@ def apply_metadrive_patches(arrive_dest_done=True): # By default, metadrive won't try to use cuda images unless it's used as a sensor for vehicles, so patch that in def add_image_sensor_patched(self, name: str, cls, args): if self.global_config["image_on_cuda"]:# and name == self.global_config["vehicle_config"]["image_source"]: - sensor = cls(*args, self, cuda=True) + sensor = cls(*args, self, cuda=True) else: - sensor = cls(*args, self, cuda=False) + sensor = cls(*args, self, cuda=False) assert isinstance(sensor, ImageBuffer), "This API is for adding image sensor" self.sensors[name] = sensor diff --git a/scripts/lint/check_indentation.py b/scripts/lint/check_indentation.py new file mode 100755 index 0000000000..533dd2efa9 --- /dev/null +++ b/scripts/lint/check_indentation.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +import argparse +import tokenize + + +# TODO: remove this once https://github.com/astral-sh/ruff/issues/8705 is closed +def check_indentation(filename: str, indent_width: int = 2) -> bool: + failed = False + indent_stack = [0] + + with tokenize.open(filename) as f: + tokens = tokenize.generate_tokens(f.readline) + for token in tokens: + if token.type == tokenize.INDENT: + indentation = token.string + width = len(indentation) + expected = indent_stack[-1] + indent_width + + if indentation != " " * expected: + found = "indentation containing tabs" if "\t" in indentation else f"{width} spaces" + print(f"{filename}:{token.start[0]}:1: expected {expected} spaces, found {found}") + failed = True + indent_stack.append(width) + elif token.type == tokenize.DEDENT: + indent_stack.pop() + + return failed + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Check Python block indentation.") + parser.add_argument("filenames", nargs="+") + args = parser.parse_args() + + failed = False + for filename in args.filenames: + failed |= check_indentation(filename) + raise SystemExit(failed) diff --git a/scripts/lint/lint.sh b/scripts/lint/lint.sh index c678379b42..bfd93f23a9 100755 --- a/scripts/lint/lint.sh +++ b/scripts/lint/lint.sh @@ -46,6 +46,7 @@ function run_tests() { PYTHON_FILES=$2 run "ruff" ruff check openpilot --quiet + run "check_indentation" $DIR/check_indentation.py $PYTHON_FILES run "check_added_large_files" $DIR/check_added_large_files.py --maxkb=120 $ALL_FILES run "check_shebang_scripts_are_executable" $DIR/check_shebang_scripts_are_executable.py $ALL_FILES run "check_shebang_format" $DIR/check_shebang_format.sh $ALL_FILES @@ -66,6 +67,7 @@ function help() { echo "" echo -e "${BOLD}${UNDERLINE}Tests:${NC}" echo -e " ${BOLD}ruff${NC}" + echo -e " ${BOLD}check_indentation${NC}" echo -e " ${BOLD}ty${NC}" echo -e " ${BOLD}codespell${NC}" echo -e " ${BOLD}check_added_large_files${NC}"