From 1d5fcb31ab0e6bbb9aa5ff1646273e71cfdee868 Mon Sep 17 00:00:00 2001 From: Jan Beran Date: Mon, 22 Jan 2024 12:49:59 +0100 Subject: [PATCH] feat: Use ruff instead of flake8 and black both in pre-commit and CI --- .gitlab-ci.yml | 8 ++--- .pre-commit-config.yaml | 13 +++---- .ruff.toml | 59 ++++++++++++++++++++++++++++++++ espefuse/efuse/base_fields.py | 5 +-- espefuse/efuse/esp32c2/fields.py | 5 ++- esptool/bin_image.py | 2 +- esptool/cmds.py | 5 +-- setup.cfg | 27 --------------- setup.py | 5 +-- 9 files changed, 78 insertions(+), 51 deletions(-) create mode 100644 .ruff.toml delete mode 100644 setup.cfg diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2a193172b..423ddbf14 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -128,12 +128,12 @@ check_python_style: codequality: code_quality_report.json when: on_failure script: - # This step installs any 'dev' dependencies (ie flake8, Black) + # This step installs any 'dev' dependencies (ie ruff) # The runner should cache the downloads, so still quite fast. - pip install -e .[dev] --prefer-binary - - python -m flake8 --exit-zero --format gl-codeclimate --output-file code_quality_report.json - - python -m flake8 - - python -m black --check --diff . + - python -m ruff check --exit-zero --output-format=gitlab --output-file=code_quality_report.json + - python -m ruff check + - python -m ruff format .run_esptool: &run_esptool | esptool.py --help diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 04f91d000..bc3f02815 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,13 +1,10 @@ repos: - - repo: https://github.com/PyCQA/flake8 - rev: 6.1.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.14 hooks: - - id: flake8 - additional_dependencies: [flake8-import-order] - - repo: https://github.com/psf/black - rev: 23.11.0 - hooks: - - id: black + - id: ruff # Runs ruff linter (replaces flake8) + args: [--fix, --exit-non-zero-on-fix] # --fix for fixing errors + - id: ruff-format - repo: https://github.com/espressif/conventional-precommit-linter rev: v1.4.0 hooks: diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 000000000..1a1d17fda --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,59 @@ +# https://docs.astral.sh/ruff/settings/ +# Exclude a variety of commonly ignored directories. +exclude = [ + ".eggs", + ".git", + "__pycache__" +] + +line-length = 88 + +select = ['E', 'F', 'W'] +ignore = ["E203"] + +# Assume Python 3.7 +target-version = "py37" + +[per-file-ignores] + + +# tests often manipulate sys.path before importing the main tools, so ignore import order violations +"test/*.py" = ["E402"] + +# multiple spaces after ',' and long lines - used for visual layout of eFuse data +"espefuse/efuse/*/mem_definition.py" = ["E241", "E501"] +"espefuse/efuse/*/operations.py" = ["E241", "E501", "F401"] +"espefuse/efuse/*/fields.py" = ["E241", "E501"] + +# ignore long lines - used for RS encoding pairs +"test/test_modules.py" = ["E501"] + +# don't check for unused imports in __init__.py files +"__init__.py" = ["F401"] + +# allow definition from star imports in docs config +"docs/conf_common.py" = ["F405"] + + + +[lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +# McCabe complexity (`C901`) by default. +select = ["E4", "E7", "E9", "F"] +ignore = [] + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + + +# ruff-format hook configuration +[format] +quote-style = "double" +indent-style = "space" +docstring-code-format = true + diff --git a/espefuse/efuse/base_fields.py b/espefuse/efuse/base_fields.py index 8ad6616f9..13ce90b2c 100644 --- a/espefuse/efuse/base_fields.py +++ b/espefuse/efuse/base_fields.py @@ -58,8 +58,9 @@ def check_arg_value(efuse, new_value): elif efuse.efuse_type.startswith("bytes"): if new_value is None: raise esptool.FatalError( - "New value required for efuse '{}' " - "(given None)".format(efuse.name) + "New value required for efuse '{}' (given None)".format( + efuse.name + ) ) if len(new_value) * 8 != efuse.bitarray.len: raise esptool.FatalError( diff --git a/espefuse/efuse/esp32c2/fields.py b/espefuse/efuse/esp32c2/fields.py index cba599e63..bfbcae632 100644 --- a/espefuse/efuse/esp32c2/fields.py +++ b/espefuse/efuse/esp32c2/fields.py @@ -368,15 +368,14 @@ def print_field(e, new_value): class EfuseKeyPurposeField(EfuseField): + # fmt: off KEY_PURPOSES = [ - # fmt: off ("USER", 0, None), # User purposes (software-only use) ("XTS_AES_128_KEY", 1, None), # (whole 256bits) flash/PSRAM encryption ("XTS_AES_128_KEY_DERIVED_FROM_128_EFUSE_BITS", 2, None), # (lo 128bits) flash/PSRAM encryption ("SECURE_BOOT_DIGEST", 3, "DIGEST"), # (hi 128bits) Secure Boot key digest - # fmt: on - ] + ] # fmt: on KEY_PURPOSES_NAME = [name[0] for name in KEY_PURPOSES] DIGEST_KEY_PURPOSES = [name[0] for name in KEY_PURPOSES if name[2] == "DIGEST"] diff --git a/esptool/bin_image.py b/esptool/bin_image.py index d260ef92a..7ce2204bd 100644 --- a/esptool/bin_image.py +++ b/esptool/bin_image.py @@ -1252,7 +1252,7 @@ def read_section_header(offs): nobits_secitons = [s for s in all_sections if s[1] == ELFFile.SEC_TYPE_NOBITS] # search for the string table section - if not (shstrndx * self.LEN_SEC_HEADER) in section_header_offsets: + if (shstrndx * self.LEN_SEC_HEADER) not in section_header_offsets: raise FatalError("ELF file has no STRTAB section at shstrndx %d" % shstrndx) _, sec_type, _, sec_size, sec_offs = read_section_header( shstrndx * self.LEN_SEC_HEADER diff --git a/esptool/cmds.py b/esptool/cmds.py index a12427112..29b0b3993 100644 --- a/esptool/cmds.py +++ b/esptool/cmds.py @@ -892,8 +892,9 @@ def get_key_from_value(dict, val): ESP8266V2FirmwareImage.IMAGE_V2_MAGIC, ]: raise FatalError( - "This is not a valid image " - "(invalid magic number: {:#x})".format(magic) + "This is not a valid image " "(invalid magic number: {:#x})".format( + magic + ) ) if args.chip == "auto": diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 67c9fa70a..000000000 --- a/setup.cfg +++ /dev/null @@ -1,27 +0,0 @@ -[flake8] -exclude = .git,__pycache__,.eggs,build -ignore = - # multiple spaces before operator - used for visual indent of constants in some files - E221, - -per-file-ignores = - # tests often manipulate sys.path before importing the main tools, so ignore import order violations - test/*.py: E402, - - # multiple spaces after ',' and long lines - used for visual layout of eFuse data - espefuse/efuse/*/mem_definition.py: E241, E501, - espefuse/efuse/*/operations.py: E241, E501, F401, - espefuse/efuse/*/fields.py: E241, E501, - - # ignore long lines - used for RS encoding pairs - test/test_modules.py: E501, - - # don't check for unused imports in __init__.py files - __init__.py: F401, - - # allow definition from star imports in docs config - docs/conf_common.py: F405, - -# Compatibility with Black -max-line-length = 88 -extend-ignore = E203, W503, diff --git a/setup.py b/setup.py index 39d88e02c..dfa428c8d 100644 --- a/setup.py +++ b/setup.py @@ -108,12 +108,9 @@ def find_version(*file_paths): setup_requires=(["wheel"] if "bdist_wheel" in sys.argv else []), extras_require={ "dev": [ - "flake8>=3.2.0", - "flake8-import-order", - "flake8-gl-codeclimate", + "ruff>=0.1.14", "pyelftools", "coverage~=6.0", - "black", "pre-commit", "pytest", "pytest-rerunfailures",