Skip to content

Commit cee0945

Browse files
jimmodpgeorge
authored andcommitted
all: Replace "black" with "ruff format".
- Add config for [tool.ruff.format] to pyproject.toml. - Update pre-commit to run both ruff and ruff-format. - Update a small number of files that change with ruff's rules. - Update CI. - Simplify codeformat.py just forward directly to "ruff format" This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <[email protected]>
1 parent ad0a259 commit cee0945

File tree

9 files changed

+20
-119
lines changed

9 files changed

+20
-119
lines changed

.github/workflows/code_formatting.yml

-16
This file was deleted.

.github/workflows/ruff.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
2-
name: Python code lint with ruff
2+
name: Python code lint and formatting with ruff
33
on: [push, pull_request]
44
jobs:
55
ruff:
66
runs-on: ubuntu-latest
77
steps:
88
- uses: actions/checkout@v4
9-
- run: pip install --user ruff==0.1.0
9+
- run: pip install --user ruff==0.1.2
1010
- run: ruff check --output-format=github .
11+
- run: ruff format --diff .

.pre-commit-config.yaml

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
repos:
22
- repo: local
33
hooks:
4-
- id: codeformat
5-
name: MicroPython codeformat.py for changed files
6-
entry: tools/codeformat.py -v -f
7-
language: python
84
- id: verifygitlog
95
name: MicroPython git commit message format checker
106
entry: tools/verifygitlog.py --check-file --ignore-rebase
117
language: python
128
verbose: true
139
stages: [commit-msg]
1410
- repo: https://github.com/charliermarsh/ruff-pre-commit
15-
rev: v0.1.0
11+
rev: v0.1.2
1612
hooks:
1713
- id: ruff
14+
id: ruff-format

micropython/aiorepl/aiorepl.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ async def __code():
3939
{}
4040
4141
__exec_task = asyncio.create_task(__code())
42-
""".format(
43-
code
44-
)
42+
""".format(code)
4543

4644
async def kbd_intr_task(exec_task, s):
4745
while True:

pyproject.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ ignore = [
6161
"F401",
6262
"F403",
6363
"F405",
64+
"E501",
6465
"F541",
6566
"F841",
67+
"ISC001",
6668
"ISC003", # micropython does not support implicit concatenation of f-strings
6769
"PIE810", # micropython does not support passing tuples to .startswith or .endswith
6870
"PLC1901",
@@ -74,8 +76,9 @@ ignore = [
7476
"PLW2901",
7577
"RUF012",
7678
"RUF100",
79+
"W191",
7780
]
78-
line-length = 260
81+
line-length = 99
7982
target-version = "py37"
8083

8184
[tool.ruff.mccabe]
@@ -97,3 +100,5 @@ max-statements = 166
97100

98101
# ble multitests are evaluated with some names pre-defined
99102
"micropython/bluetooth/aioble/multitests/*" = ["F821"]
103+
104+
[tool.ruff.format]

python-ecosys/cbor2/cbor2/decoder.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ def read(self, amount):
210210
data = self.fp.read(amount)
211211
if len(data) < amount:
212212
raise CBORDecodeError(
213-
"premature end of stream (expected to read {} bytes, got {} "
214-
"instead)".format(amount, len(data))
213+
"premature end of stream (expected to read {} bytes, got {} instead)".format(
214+
amount, len(data)
215+
)
215216
)
216217

217218
return data

tools/ci.sh

-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
#!/bin/bash
22

3-
########################################################################################
4-
# code formatting
5-
6-
function ci_code_formatting_setup {
7-
sudo apt-add-repository --yes --update ppa:pybricks/ppa
8-
sudo apt-get install uncrustify
9-
pip3 install black
10-
uncrustify --version
11-
black --version
12-
}
13-
14-
function ci_code_formatting_run {
15-
tools/codeformat.py -v
16-
}
17-
183
########################################################################################
194
# commit formatting
205

tools/codeformat.py

+4-72
Original file line numberDiff line numberDiff line change
@@ -25,87 +25,19 @@
2525
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2626
# THE SOFTWARE.
2727

28-
# This is based on tools/codeformat.py from the main micropython/micropython
29-
# repository but without support for .c/.h files.
28+
# This is just a wrapper around running ruff format, so that code formatting can be
29+
# invoked in the same way as in the main repo.
3030

31-
import argparse
32-
import glob
33-
import itertools
3431
import os
35-
import re
3632
import subprocess
3733

38-
# Relative to top-level repo dir.
39-
PATHS = [
40-
"**/*.py",
41-
]
42-
43-
EXCLUSIONS = []
44-
4534
# Path to repo top-level dir.
4635
TOP = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
4736

48-
PY_EXTS = (".py",)
49-
50-
51-
def list_files(paths, exclusions=None, prefix=""):
52-
files = set()
53-
for pattern in paths:
54-
files.update(glob.glob(os.path.join(prefix, pattern), recursive=True))
55-
for pattern in exclusions or []:
56-
files.difference_update(glob.fnmatch.filter(files, os.path.join(prefix, pattern)))
57-
return sorted(files)
58-
5937

6038
def main():
61-
cmd_parser = argparse.ArgumentParser(description="Auto-format Python files.")
62-
cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output")
63-
cmd_parser.add_argument(
64-
"-f",
65-
action="store_true",
66-
help="Filter files provided on the command line against the default list of files to check.",
67-
)
68-
cmd_parser.add_argument("files", nargs="*", help="Run on specific globs")
69-
args = cmd_parser.parse_args()
70-
71-
# Expand the globs passed on the command line, or use the default globs above.
72-
files = []
73-
if args.files:
74-
files = list_files(args.files)
75-
if args.f:
76-
# Filter against the default list of files. This is a little fiddly
77-
# because we need to apply both the inclusion globs given in PATHS
78-
# as well as the EXCLUSIONS, and use absolute paths
79-
files = {os.path.abspath(f) for f in files}
80-
all_files = set(list_files(PATHS, EXCLUSIONS, TOP))
81-
if args.v: # In verbose mode, log any files we're skipping
82-
for f in files - all_files:
83-
print("Not checking: {}".format(f))
84-
files = list(files & all_files)
85-
else:
86-
files = list_files(PATHS, EXCLUSIONS, TOP)
87-
88-
# Extract files matching a specific language.
89-
def lang_files(exts):
90-
for file in files:
91-
if os.path.splitext(file)[1].lower() in exts:
92-
yield file
93-
94-
# Run tool on N files at a time (to avoid making the command line too long).
95-
def batch(cmd, files, N=200):
96-
while True:
97-
file_args = list(itertools.islice(files, N))
98-
if not file_args:
99-
break
100-
subprocess.check_call(cmd + file_args)
101-
102-
# Format Python files with black.
103-
command = ["black", "--fast", "--line-length=99"]
104-
if args.v:
105-
command.append("-v")
106-
else:
107-
command.append("-q")
108-
batch(command, lang_files(PY_EXTS))
39+
command = ["ruff", "format", "."]
40+
subprocess.check_call(command, cwd=TOP)
10941

11042

11143
if __name__ == "__main__":

tools/makepyproject.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ def build(manifest_path, output_path):
185185
"""
186186
[tool.hatch.build]
187187
packages = ["{}"]
188-
""".format(
189-
top_level_package
190-
),
188+
""".format(top_level_package),
191189
file=toml_file,
192190
)
193191

0 commit comments

Comments
 (0)