diff --git a/.github/workflows/code_formatting.yml b/.github/workflows/code_formatting.yml deleted file mode 100644 index 71c50aa..0000000 --- a/.github/workflows/code_formatting.yml +++ /dev/null @@ -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 diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml index 0374d76..71c4131 100644 --- a/.github/workflows/ruff.yml +++ b/.github/workflows/ruff.yml @@ -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 . diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bfce6a2..335c1c2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,6 @@ 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 @@ -12,6 +8,7 @@ repos: 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 diff --git a/micropython/aiorepl/aiorepl.py b/micropython/aiorepl/aiorepl.py index 8b3ce4f..e7e3167 100644 --- a/micropython/aiorepl/aiorepl.py +++ b/micropython/aiorepl/aiorepl.py @@ -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: diff --git a/pyproject.toml b/pyproject.toml index 1aa9c11..3b25245 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", @@ -74,8 +76,9 @@ ignore = [ "PLW2901", "RUF012", "RUF100", + "W191", ] -line-length = 260 +line-length = 99 target-version = "py37" [tool.ruff.mccabe] @@ -97,3 +100,5 @@ max-statements = 166 # ble multitests are evaluated with some names pre-defined "micropython/bluetooth/aioble/multitests/*" = ["F821"] + +[tool.ruff.format] diff --git a/python-ecosys/cbor2/cbor2/decoder.py b/python-ecosys/cbor2/cbor2/decoder.py index 48ff02d..e38f078 100644 --- a/python-ecosys/cbor2/cbor2/decoder.py +++ b/python-ecosys/cbor2/cbor2/decoder.py @@ -210,8 +210,9 @@ class CBORDecoder(object): 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 diff --git a/tools/ci.sh b/tools/ci.sh index 1398947..730034e 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -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 diff --git a/tools/codeformat.py b/tools/codeformat.py index 2bc0c7f..6a7f2b3 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -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__": diff --git a/tools/makepyproject.py b/tools/makepyproject.py index eaaef01..25c05d0 100755 --- a/tools/makepyproject.py +++ b/tools/makepyproject.py @@ -185,9 +185,7 @@ urls = {{ Homepage = "https://github.com/micropython/micropython-lib" }} """ [tool.hatch.build] packages = ["{}"] -""".format( - top_level_package - ), +""".format(top_level_package), file=toml_file, )