lint: check indentation (#38454)

* lint: check indentation

* cleanup

* happy

* that was too much

* happy
This commit is contained in:
Adeeb Shihadeh
2026-07-25 10:29:37 -07:00
committed by GitHub
parent c37855d113
commit 27122bbd28
9 changed files with 79 additions and 39 deletions
+38
View File
@@ -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)
+2
View File
@@ -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}"