Skip to content

Commit 9d2ae4e

Browse files
committed
Switch to ruff like MicroPython
1 parent 7f0cc9e commit 9d2ae4e

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,19 @@ repos:
3939
- id: formatting
4040
name: Formatting
4141
entry: python3 tools/codeformat.py
42-
types_or: [c, python]
42+
types: [c]
4343
language: system
4444
exclude: |
4545
(?x)^(
4646
lib/tinyusb|
4747
ports/raspberrypi/sdk
4848
)
49+
- repo: https://github.com/astral-sh/ruff-pre-commit
50+
# Ruff version.
51+
rev: v0.9.4
52+
hooks:
53+
# Run the linter.
54+
- id: ruff
55+
args: [ --fix ]
56+
# Run the formatter.
57+
- id: ruff-format

pyproject.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,38 @@
44

55
[tool.setuptools_scm]
66
# can be empty if no extra settings are needed, presence enables setuptools-scm
7+
8+
# Ruff settings copied from MicroPython
9+
[tool.ruff]
10+
# Exclude third-party code from linting and formatting
11+
extend-exclude = ["lib"]
12+
line-length = 99
13+
target-version = "py37"
14+
15+
[tool.ruff.lint]
16+
extend-select = ["C9", "PLC"]
17+
ignore = [
18+
"E401",
19+
"E402",
20+
"E722",
21+
"E731",
22+
"E741",
23+
"F401",
24+
"F403",
25+
"F405",
26+
"PLC1901",
27+
]
28+
29+
[tool.ruff.mccabe]
30+
max-complexity = 40
31+
32+
[tool.ruff.lint.per-file-ignores]
33+
# Exclude all tests from linting (does not apply to formatting).
34+
"tests/**/*.py" = ["ALL"]
35+
36+
[tool.ruff.format]
37+
# Exclude third-party code, and exclude the following tests:
38+
# basics: needs careful attention before applying automatic formatting
39+
# repl_: not real python files
40+
# viper_args: uses f(*)
41+
exclude = ["tests/basics/*.py", "tests/*/repl_*.py", "tests/micropython/viper_args.py"]

tools/codeformat.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
# Relative to top-level repo dir.
4040
# CIRCUITPY-CHANGE: different directory trees
4141
PATHS = [
42-
# C
4342
"main.c",
4443
"devices/**/*.[ch]",
4544
"extmod/*.[ch]",
@@ -52,26 +51,14 @@
5251
"shared-bindings/**/*.[ch]",
5352
"shared-module/**/*.[ch]",
5453
"supervisor/**/*.[ch]",
55-
# Python
56-
"extmod/*.py",
57-
"ports/**/*.py",
58-
"py/**/*.py",
59-
"tools/**/*.py",
60-
"tests/circuitpython-*/**/*.py",
6154
]
6255

6356
# CIRCUITPY-CHANGE: different exclusions
6457
EXCLUSIONS = [
6558
# STM32 build includes generated Python code.
6659
"ports/*/build*",
67-
# gitignore in ports/unix ignores *.py, so also do it here.
68-
"ports/unix/**/*.py",
69-
# not real python files
70-
"tests/**/repl_*.py",
7160
# don't reindent this third-party code we vendored in
7261
"ports/raspberrypi/lwip_src",
73-
# line breaks
74-
"tools/mpy-tool.py",
7562
]
7663

7764

@@ -119,12 +106,6 @@ def transform(m):
119106
TOP = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
120107

121108
UNCRUSTIFY_CFG = os.path.join(TOP, "tools/uncrustify.cfg")
122-
# CIRCUITPY-CHANGE
123-
C_EXTS = (
124-
".c",
125-
".h",
126-
)
127-
PY_EXTS = (".py",)
128109

129110

130111
def check_uncrustify_version():
@@ -217,14 +198,8 @@ def main():
217198
# Expand the arguments passed on the command line, subject to the PATHS and EXCLUSIONS
218199
files = list_files(args.files)
219200

220-
# Extract files matching a specific language.
221-
def lang_files(exts):
222-
for file in files:
223-
if os.path.splitext(file)[1].lower() in exts:
224-
yield file
225-
226201
def bindings_files():
227-
for file in lang_files(C_EXTS):
202+
for file in files:
228203
if file.startswith("shared-bindings/") or "/bindings/" in file:
229204
yield file
230205

@@ -248,8 +223,8 @@ def batch(cmd, files, N=200, check=False):
248223
command = ["uncrustify", "-c", UNCRUSTIFY_CFG, "-lC", "--no-backup"]
249224
if not args.v:
250225
command.append("-q")
251-
batch(command, lang_files(C_EXTS))
252-
for file in lang_files(C_EXTS):
226+
batch(command, files)
227+
for file in files:
253228
fixup_c(file)
254229
# Format bindings with black_bindings
255230
if format_py:
@@ -258,12 +233,13 @@ def batch(cmd, files, N=200, check=False):
258233

259234
# Format Python files with black.
260235
if format_py:
261-
command = ["black", "--fast", "--line-length=99"]
236+
command = ["rust", "format"]
262237
if args.v:
263238
command.append("-v")
264239
else:
265240
command.append("-q")
266-
batch(command, lang_files(PY_EXTS))
241+
command.append(".")
242+
subprocess.check_call(command, cwd=TOP)
267243

268244

269245
if __name__ == "__main__":

0 commit comments

Comments
 (0)