Skip to content

Commit

Permalink
[CI] Migrate from flake8, black, and isort to ruff (#164)
Browse files Browse the repository at this point in the history
* [REF] Increase batch sizes, update results with more efficient matvecs

* [ADD] Update benchmark figures

* [CI] Migrate linter from `flake8` to `ruff`

* [DOC] Update changelog

* [FMT] Apply `black`

* [CI] Ignore PLR2004 and PLW2901

* [CI] Use `ruff format` instead of `black`

* [CI] Use ruff for import sorting

* [CI] Minor fixes

* [DOC] Update changelog

* [REF] Delete noqas of PLR2004

* [CI] Small fix

* [REF] Sort imports
  • Loading branch information
f-dangel authored Jan 9, 2025
1 parent d65efd3 commit c49580a
Show file tree
Hide file tree
Showing 29 changed files with 162 additions and 218 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/lint-darglint.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
name: Lint-darglint

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'

on: [push, pull_request]

jobs:
darglint:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
name: Lint-flake8
name: Lint-format

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'
on: [push, pull_request]

jobs:
flake8:
ruff-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -22,6 +16,6 @@ jobs:
run: |
python -m pip install --upgrade pip
make install-lint
- name: Run flake8
- name: Run ruff format
run: |
make flake8
make ruff-format-check
28 changes: 0 additions & 28 deletions .github/workflows/lint-isort.yaml

This file was deleted.

8 changes: 1 addition & 7 deletions .github/workflows/lint-pydocstyle.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
name: Lint-pydocstyle

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'
on: [push, pull_request]

jobs:
pydocstyle:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
name: Lint-black

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'
name: Lint-ruff

on: [push, pull_request]

jobs:
black:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -23,6 +16,6 @@ jobs:
run: |
python -m pip install --upgrade pip
make install-lint
- name: Run black
- name: Run ruff
run: |
make black-check
make ruff-check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ src/
.vscode/*
.coverage
.eggs
**.DS_Store
16 changes: 0 additions & 16 deletions black.toml

This file was deleted.

2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Always run full tests
([PR](https://github.com/f-dangel/curvlinops/pull/161))
- Migrate linting, formatting, and import sorting from `flake8`, `black`, and
`isort` to `ruff` ([PR](https://github.com/f-dangel/curvlinops/pull/164))

- Modify logo
([PR](https://github.com/f-dangel/curvlinops/pull/169))
Expand Down
6 changes: 4 additions & 2 deletions curvlinops/inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ def _compute_inverse_factors(
warn(
f"Failed to compute Cholesky decomposition in {aaT.dtype} "
f"precision with error {error}. "
"Retrying in double precision..."
"Retrying in double precision...",
stacklevel=2,
)
# Retry in double precision
original_type = aaT.dtype
Expand All @@ -476,7 +477,8 @@ def _compute_inverse_factors(
warn(
f"Failed to compute Cholesky decomposition in {ggT.dtype} "
f"precision with error {error}. "
"Retrying in double precision..."
"Retrying in double precision...",
stacklevel=2,
)
# Retry in double precision
original_dtype = ggT.dtype
Expand Down
7 changes: 3 additions & 4 deletions docs/examples/basic_usage/example_eigenvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ def orthonormalize(v: numpy.ndarray, basis: List[numpy.ndarray]) -> numpy.ndarra

if eigenvalue is None:
eigenvalue = tmp_eigenvalue
elif abs(eigenvalue - tmp_eigenvalue) / (abs(eigenvalue) + 1e-6) < tol:
break
else:
if abs(eigenvalue - tmp_eigenvalue) / (abs(eigenvalue) + 1e-6) < tol:
break
else:
eigenvalue = tmp_eigenvalue
eigenvalue = tmp_eigenvalue

eigenvalues.append(eigenvalue)
eigenvectors.append(v)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/basic_usage/memory_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from curvlinops import KFACInverseLinearOperator, KFACLinearOperator


def run_peakmem_benchmark( # noqa: C901
def run_peakmem_benchmark( # noqa: C901, PLR0915
linop_str: str, problem_str: str, device_str: str, op_str: str
):
"""Execute the memory benchmark for a given linear operator class and save results.
Expand Down
50 changes: 21 additions & 29 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ help:
@echo "install"
@echo " Install curvlinops and dependencies"
@echo "uninstall"
@echo " Unstall curvlinops"
@echo " Uninstall curvlinops"
@echo "lint"
@echo " Run all linting actions"
@echo "docs"
Expand All @@ -21,16 +21,14 @@ help:
@echo " Run pytest on test and report coverage"
@echo "install-lint"
@echo " Install curvlinops and the linter tools"
@echo "isort"
@echo " Run isort (sort imports) on the project"
@echo "isort-check"
@echo " Check if isort (sort imports) would change files"
@echo "black"
@echo " Run black on the project"
@echo "black-check"
@echo " Check if black would change files"
@echo "flake8"
@echo " Run flake8 on the project"
@echo "ruff-format"
@echo " Run ruff format on the project"
@echo "ruff-format-check"
@echo " Check if ruff format would change files"
@echo "ruff"
@echo " Run ruff on the project and fix errors"
@echo "ruff-check"
@echo " Run ruff check on the project without fixing errors"
@echo "conda-env"
@echo " Create conda environment 'curvlinops' with dev setup"
@echo "darglint-check"
Expand Down Expand Up @@ -81,26 +79,21 @@ test-light:
install-lint:
@pip install -e .[lint]

.PHONY: isort isort-check
.PHONY: ruff-format ruff-format-check

isort:
@isort .
ruff-format:
@ruff format .

isort-check:
@isort . --check --diff
ruff-format-check:
@ruff format --check .

.PHONY: black black-check
.PHONY: ruff-check

black:
@black . --config=black.toml
ruff:
@ruff check . --fix

black-check:
@black . --config=black.toml --check

.PHONY: flake8

flake8:
@flake8 .
ruff-check:
@ruff check .

.PHONY: darglint-check

Expand All @@ -120,8 +113,7 @@ conda-env:
.PHONY: lint

lint:
make black-check
make isort-check
make flake8
make ruff-format-check
make ruff-check
make darglint-check
make pydocstyle-check
69 changes: 52 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,9 @@ test = [

# Dependencies needed for linting.
lint = [
"black",
"flake8",
"mccabe",
"pycodestyle",
"pyflakes",
"pep8-naming",
"flake8-bugbear",
"flake8-comprehensions",
"flake8-tidy-imports",
"ruff",
"darglint",
"pydocstyle",
"isort",
]

# Dependencies needed to build/view the documentation.
Expand All @@ -95,14 +86,58 @@ docs = [
###############################################################################
[tool.setuptools_scm]

[tool.isort]
profile = "black"
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true

[tool.pydocstyle]
convention = "google"
match = '.*\.py'
match_dir = '^(?!(test|.git)).*'

[tool.ruff]
line-length = 88

[tool.ruff.lint]
# Enable all rules from flake8 (E, F), plus additional ones including isort (I)
select = ["E", "F", "B", "C", "W", "B9", "PLE", "PLW", "PLR", "I"]
ignore = [
# E501 max-line-length (replaced by B950 (max-line-length + 10%))
"E501",
# C408 use {} instead of dict() (ignored because pytorch uses dict)
"C408",
# E203 whitespace before :
"E203",
# E231 missing whitespace after ','
"E231",
# W291 trailing whitespace
"W291",
# E203 line break before binary operator (replaces W503)
"E203",
# Line break occurred after a binary operator (replaces W504)
"E226",
# B905 `zip()` without an explicit `strict=` parameter
"B905",
# Too many arguments in function definition (9 > 5)
"PLR0913",
# Magic value comparison
"PLR2004",
# Loop variable overwritten by assignment target
"PLW2901",
]

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
exclude = [
".eggs",
".git",
".pytest_cache",
"docs/rtd",
"build",
"dist",
]

[tool.ruff.lint.per-file-ignores]
# Add any per-file ignores here if needed

[tool.ruff.lint.flake8-bugbear]
extend-immutable-calls = ["pytest.raises", "pytest.warns", "pytest.mark.skip"]
23 changes: 0 additions & 23 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
# Note: These tools do not yet support `pyproject.toml`, but these options
# should be moved there once support is added.

[flake8]
select = B,C,E,F,P,W,B9
max-line-length = 88
max-complexity = 10
# E501 max-line-length (replaced by B950 (max-line-length + 10%))
# C408 use {} instead of dict() (ignored because pytorch uses dict)
# E203 whitespace before : (not Black-compatible)
# E231 missing whitespace after ','
# W291 trailing whitespace
# W503 line break before binary operator
# W504 line break after binary operator
# B905 `zip()` without an explicit `strict=` parameter
ignore =
E501,
C408,
E203,
E231,
W291,
W503,
W504,
B905,
exclude = build, .git, docs/rtd, .eggs

[darglint]
docstring_style = google
strictness = full
Loading

0 comments on commit c49580a

Please sign in to comment.