Skip to content

Commit

Permalink
all: Replace "black" with "ruff format".
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
jimmo authored and dpgeorge committed Dec 20, 2023
1 parent ad0a259 commit cee0945
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 119 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/code_formatting.yml

This file was deleted.

5 changes: 3 additions & 2 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
name: Python code lint with ruff
name: Python code lint and formatting with ruff
on: [push, pull_request]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: pip install --user ruff==0.1.0
- run: pip install --user ruff==0.1.2
- run: ruff check --output-format=github .
- run: ruff format --diff .
7 changes: 2 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
repos:
- repo: local
hooks:
- id: codeformat
name: MicroPython codeformat.py for changed files
entry: tools/codeformat.py -v -f
language: python
- id: verifygitlog
name: MicroPython git commit message format checker
entry: tools/verifygitlog.py --check-file --ignore-rebase
language: python
verbose: true
stages: [commit-msg]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.0
rev: v0.1.2
hooks:
- id: ruff
id: ruff-format
4 changes: 1 addition & 3 deletions micropython/aiorepl/aiorepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ async def __code():
{}
__exec_task = asyncio.create_task(__code())
""".format(
code
)
""".format(code)

async def kbd_intr_task(exec_task, s):
while True:
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ ignore = [
"F401",
"F403",
"F405",
"E501",
"F541",
"F841",
"ISC001",
"ISC003", # micropython does not support implicit concatenation of f-strings
"PIE810", # micropython does not support passing tuples to .startswith or .endswith
"PLC1901",
Expand All @@ -74,8 +76,9 @@ ignore = [
"PLW2901",
"RUF012",
"RUF100",
"W191",
]
line-length = 260
line-length = 99
target-version = "py37"

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

# ble multitests are evaluated with some names pre-defined
"micropython/bluetooth/aioble/multitests/*" = ["F821"]

[tool.ruff.format]
5 changes: 3 additions & 2 deletions python-ecosys/cbor2/cbor2/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ def read(self, amount):
data = self.fp.read(amount)
if len(data) < amount:
raise CBORDecodeError(
"premature end of stream (expected to read {} bytes, got {} "
"instead)".format(amount, len(data))
"premature end of stream (expected to read {} bytes, got {} instead)".format(
amount, len(data)
)
)

return data
Expand Down
15 changes: 0 additions & 15 deletions tools/ci.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
#!/bin/bash

########################################################################################
# code formatting

function ci_code_formatting_setup {
sudo apt-add-repository --yes --update ppa:pybricks/ppa
sudo apt-get install uncrustify
pip3 install black
uncrustify --version
black --version
}

function ci_code_formatting_run {
tools/codeformat.py -v
}

########################################################################################
# commit formatting

Expand Down
76 changes: 4 additions & 72 deletions tools/codeformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,87 +25,19 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

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

import argparse
import glob
import itertools
import os
import re
import subprocess

# Relative to top-level repo dir.
PATHS = [
"**/*.py",
]

EXCLUSIONS = []

# Path to repo top-level dir.
TOP = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))

PY_EXTS = (".py",)


def list_files(paths, exclusions=None, prefix=""):
files = set()
for pattern in paths:
files.update(glob.glob(os.path.join(prefix, pattern), recursive=True))
for pattern in exclusions or []:
files.difference_update(glob.fnmatch.filter(files, os.path.join(prefix, pattern)))
return sorted(files)


def main():
cmd_parser = argparse.ArgumentParser(description="Auto-format Python files.")
cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output")
cmd_parser.add_argument(
"-f",
action="store_true",
help="Filter files provided on the command line against the default list of files to check.",
)
cmd_parser.add_argument("files", nargs="*", help="Run on specific globs")
args = cmd_parser.parse_args()

# Expand the globs passed on the command line, or use the default globs above.
files = []
if args.files:
files = list_files(args.files)
if args.f:
# Filter against the default list of files. This is a little fiddly
# because we need to apply both the inclusion globs given in PATHS
# as well as the EXCLUSIONS, and use absolute paths
files = {os.path.abspath(f) for f in files}
all_files = set(list_files(PATHS, EXCLUSIONS, TOP))
if args.v: # In verbose mode, log any files we're skipping
for f in files - all_files:
print("Not checking: {}".format(f))
files = list(files & all_files)
else:
files = list_files(PATHS, EXCLUSIONS, TOP)

# Extract files matching a specific language.
def lang_files(exts):
for file in files:
if os.path.splitext(file)[1].lower() in exts:
yield file

# Run tool on N files at a time (to avoid making the command line too long).
def batch(cmd, files, N=200):
while True:
file_args = list(itertools.islice(files, N))
if not file_args:
break
subprocess.check_call(cmd + file_args)

# Format Python files with black.
command = ["black", "--fast", "--line-length=99"]
if args.v:
command.append("-v")
else:
command.append("-q")
batch(command, lang_files(PY_EXTS))
command = ["ruff", "format", "."]
subprocess.check_call(command, cwd=TOP)


if __name__ == "__main__":
Expand Down
4 changes: 1 addition & 3 deletions tools/makepyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ def build(manifest_path, output_path):
"""
[tool.hatch.build]
packages = ["{}"]
""".format(
top_level_package
),
""".format(top_level_package),
file=toml_file,
)

Expand Down

0 comments on commit cee0945

Please sign in to comment.