From 0f7dc74edcc0875ef44d015516b6c00d0f4d4733 Mon Sep 17 00:00:00 2001 From: "Shah, Karan" Date: Thu, 28 Nov 2024 22:00:43 +0530 Subject: [PATCH] Migrate to ruff tooling and config Signed-off-by: Shah, Karan --- CONTRIBUTING.md | 2 +- linters-requirements.txt | 4 +--- pyproject.toml | 30 +++++++++++++++++++++++++----- setup.cfg | 39 --------------------------------------- shell/format.sh | 6 ++---- shell/lint.sh | 6 ++---- 6 files changed, 31 insertions(+), 56 deletions(-) delete mode 100644 setup.cfg diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6eeb6f5a48..a89798d1e9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -60,7 +60,7 @@ pip install -r linters-requirements.txt ## Code style -OpenFL uses [black](https://black.readthedocs.io/en/stable/), [isort](https://pycqa.github.io/isort/) and [precommit](https://pre-commit.com/) to format the code. +OpenFL uses [ruff](https://github.com/astral-sh/ruff) to lint/format code and [precommit](https://pre-commit.com/) checks. Run the following command at the **root** directory of the repo to format your code. diff --git a/linters-requirements.txt b/linters-requirements.txt index f5ea6cbf18..b4a2191f21 100644 --- a/linters-requirements.txt +++ b/linters-requirements.txt @@ -1,4 +1,2 @@ -black -flake8 -isort pre-commit +ruff diff --git a/pyproject.toml b/pyproject.toml index b38d4235fd..68a42f699b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,27 @@ -[tool.black] +[tool.ruff] line-length = 100 -[tool.isort] -profile = "black" -force_single_line = "False" -line_length = 100 \ No newline at end of file +[tool.ruff.lint] +select = [ + "B", # Bandit security checks (e.g., detecting insecure function use). + "C", # Cyclomatic complexity, used to flag overly complex functions. + "E", # PEP8 errors (e.g., style issues). + "F", # Pyflakes errors, like unused imports or undefined names. + "W", # PEP8 warnings (e.g., deprecations). + "B9", # Bugbear, for additional warnings about potentially error-prone code. + "I", # isort +] +ignore = [ + "E266", # too many leading '#' for block comments + "E741", # ambiguous variable name + "E731", # do not assign a `lambda` expression, use a `def` + "B904", # ignore exception distinguishing + "B006", # mutable data structures as default args +] + +[tool.ruff.lint.per-file-ignores] +"**/__init__.py" = ["E501", "F401"] # lines too long; imported but unused + +[tool.ruff.lint.isort] +force-single-line = false +known-first-party = ["openfl"] \ No newline at end of file diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 5874a2e513..0000000000 --- a/setup.cfg +++ /dev/null @@ -1,39 +0,0 @@ -[flake8] -ignore = - # Conflicts with black - E203 - # Line break occurred before a binary operator. Update by W504 Line - W503 - # Allow "import torch.nn.functional as F" - N812 - # Line length, handled separately by max-line-length - E501 - # too many leading '#' for block comments - E266 - - -per-file-ignores = - # Unused imports in __init__.py are OK - **/__init__.py:F401 - -exclude = - *_pb2*, - .git, - __pycache__, - build, - dist, - .venv - -max-line-length = 100 - -copyright-check = True - -# Enable specific checks or plugins -# B: Bandit security checks (e.g., detecting insecure function use). -# C: Cyclomatic complexity, used to flag overly complex functions. -# E: PEP8 errors (e.g., style issues). -# F: Pyflakes errors, like unused imports or undefined names. -# W: PEP8 warnings (e.g., stylistic issues). -# T4: Type checking from third-party tools (like mypy). -# B9: Bugbear, for additional warnings about potentially error-prone code. -select = B,C,E,F,W,T4,B9 diff --git a/shell/format.sh b/shell/format.sh index 5d8f2c1547..0033b0d52c 100755 --- a/shell/format.sh +++ b/shell/format.sh @@ -6,8 +6,6 @@ base_dir=$(dirname $(dirname $0)) # Run the pre-commit checks pre-commit run --all-files -isort --sp "${base_dir}/pyproject.toml" openfl +ruff check --config "${base_dir}/pyproject.toml" --fix openfl/ -black --config "${base_dir}/pyproject.toml" openfl - -flake8 --config "${base_dir}/setup.cfg" openfl \ No newline at end of file +ruff format --config "${base_dir}/pyproject.toml" openfl/ \ No newline at end of file diff --git a/shell/lint.sh b/shell/lint.sh index f3cee9c4a1..ac0b97e0a5 100755 --- a/shell/lint.sh +++ b/shell/lint.sh @@ -6,8 +6,6 @@ base_dir=$(dirname $(dirname $0)) # Run the pre-commit checks pre-commit run --all-files -isort --sp "${base_dir}/pyproject.toml" --check openfl +ruff check --config "${base_dir}/pyproject.toml" openfl/ -black --config "${base_dir}/pyproject.toml" --check openfl - -flake8 --config "${base_dir}/setup.cfg" --show-source openfl \ No newline at end of file +ruff format --check --config "${base_dir}/pyproject.toml" openfl/ \ No newline at end of file