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 <jim.mussared@gmail.com>
pull/759/head
Jim Mussared 2023-10-17 13:18:44 +11:00 zatwierdzone przez Damien George
rodzic ad0a2590cc
commit cee0945f1c
9 zmienionych plików z 20 dodań i 119 usunięć

Wyświetl plik

@ -1,16 +0,0 @@
name: Check code formatting
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- name: Install packages
run: source tools/ci.sh && ci_code_formatting_setup
- name: Run code formatting
run: source tools/ci.sh && ci_code_formatting_run
- name: Check code formatting
run: git diff --exit-code

Wyświetl plik

@ -1,10 +1,11 @@
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python # 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] on: [push, pull_request]
jobs: jobs:
ruff: ruff:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - 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 check --output-format=github .
- run: ruff format --diff .

Wyświetl plik

@ -1,10 +1,6 @@
repos: repos:
- repo: local - repo: local
hooks: hooks:
- id: codeformat
name: MicroPython codeformat.py for changed files
entry: tools/codeformat.py -v -f
language: python
- id: verifygitlog - id: verifygitlog
name: MicroPython git commit message format checker name: MicroPython git commit message format checker
entry: tools/verifygitlog.py --check-file --ignore-rebase entry: tools/verifygitlog.py --check-file --ignore-rebase
@ -12,6 +8,7 @@ repos:
verbose: true verbose: true
stages: [commit-msg] stages: [commit-msg]
- repo: https://github.com/charliermarsh/ruff-pre-commit - repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.0 rev: v0.1.2
hooks: hooks:
- id: ruff - id: ruff
id: ruff-format

Wyświetl plik

@ -39,9 +39,7 @@ async def __code():
{} {}
__exec_task = asyncio.create_task(__code()) __exec_task = asyncio.create_task(__code())
""".format( """.format(code)
code
)
async def kbd_intr_task(exec_task, s): async def kbd_intr_task(exec_task, s):
while True: while True:

Wyświetl plik

@ -61,8 +61,10 @@ ignore = [
"F401", "F401",
"F403", "F403",
"F405", "F405",
"E501",
"F541", "F541",
"F841", "F841",
"ISC001",
"ISC003", # micropython does not support implicit concatenation of f-strings "ISC003", # micropython does not support implicit concatenation of f-strings
"PIE810", # micropython does not support passing tuples to .startswith or .endswith "PIE810", # micropython does not support passing tuples to .startswith or .endswith
"PLC1901", "PLC1901",
@ -74,8 +76,9 @@ ignore = [
"PLW2901", "PLW2901",
"RUF012", "RUF012",
"RUF100", "RUF100",
"W191",
] ]
line-length = 260 line-length = 99
target-version = "py37" target-version = "py37"
[tool.ruff.mccabe] [tool.ruff.mccabe]
@ -97,3 +100,5 @@ max-statements = 166
# ble multitests are evaluated with some names pre-defined # ble multitests are evaluated with some names pre-defined
"micropython/bluetooth/aioble/multitests/*" = ["F821"] "micropython/bluetooth/aioble/multitests/*" = ["F821"]
[tool.ruff.format]

Wyświetl plik

@ -210,8 +210,9 @@ class CBORDecoder(object):
data = self.fp.read(amount) data = self.fp.read(amount)
if len(data) < amount: if len(data) < amount:
raise CBORDecodeError( raise CBORDecodeError(
"premature end of stream (expected to read {} bytes, got {} " "premature end of stream (expected to read {} bytes, got {} instead)".format(
"instead)".format(amount, len(data)) amount, len(data)
)
) )
return data return data

Wyświetl plik

@ -1,20 +1,5 @@
#!/bin/bash #!/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 # commit formatting

Wyświetl plik

@ -25,87 +25,19 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE. # THE SOFTWARE.
# This is based on tools/codeformat.py from the main micropython/micropython # This is just a wrapper around running ruff format, so that code formatting can be
# repository but without support for .c/.h files. # invoked in the same way as in the main repo.
import argparse
import glob
import itertools
import os import os
import re
import subprocess import subprocess
# Relative to top-level repo dir.
PATHS = [
"**/*.py",
]
EXCLUSIONS = []
# Path to repo top-level dir. # Path to repo top-level dir.
TOP = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) 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(): def main():
cmd_parser = argparse.ArgumentParser(description="Auto-format Python files.") command = ["ruff", "format", "."]
cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output") subprocess.check_call(command, cwd=TOP)
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))
if __name__ == "__main__": if __name__ == "__main__":

Wyświetl plik

@ -185,9 +185,7 @@ urls = {{ Homepage = "https://github.com/micropython/micropython-lib" }}
""" """
[tool.hatch.build] [tool.hatch.build]
packages = ["{}"] packages = ["{}"]
""".format( """.format(top_level_package),
top_level_package
),
file=toml_file, file=toml_file,
) )