Skip to content

Commit

Permalink
Replace formatters with Ruff (#183)
Browse files Browse the repository at this point in the history
* Replace formatters with Ruff

* Reintroduce pydocstyle

* Update the GitHub action

* Include the format-and-lint merge of format and lint in the documentation

* Update the workflows again...
  • Loading branch information
pnkraemer authored May 24, 2024
1 parent 752d6c8 commit 24442bb
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:
- name: Install dependencies
run: |
pip install --upgrade pip
pip install .[cpu,lint]
pip install .[cpu,format-and-lint]
- name: Apply linter
run: make lint
run: make format-and-lint
test:
name: Test with ${{ matrix.python-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
Expand Down
30 changes: 14 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@ default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-merge-conflict
- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.292
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.5
hooks:
# Run the linter.
- id: ruff
args: [--fix]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
rev: v1.10.0
hooks:
- id: mypy
args: [--ignore-missing-imports]
args:
- --no-strict-optional
- --ignore-missing-imports
- --explicit-package-bases
- repo: https://github.com/lyz-code/yamlfix/
rev: 1.13.0
rev: 1.16.0
hooks:
- id: yamlfix
10 changes: 3 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
format:
isort . --quiet
black . --quiet
blackdoc *.md --line-length 82

lint:
format-and-lint:
pre-commit run --all-files
blackdoc *.md --line-length 82

test:
pytest -x -v
pytest
python -m doctest README.md
python -m doctest matfree/*.py

Expand Down
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,14 @@ make test

Install all formatting-related dependencies via
```commandline
pip install .[format]
pip install .[format-and-lint]
pre-commit install
```
and format the code via
```commandline
make format
make format-and-lint
```

To lint the code, install the pre-commit hook

```commandline
pip install .[lint]
pre-commit install
```
and run the linters via
```commandline
make lint
```

Install the documentation-related dependencies as

Expand Down Expand Up @@ -169,7 +159,7 @@ To make a pull request, proceed as follows:
- Fork the repository.
- Install all dependencies with `pip install .[full]` or `pip install -e .[full]`.
- Make your changes.
- From the root of the project, run the tests via `make test`, and check out `make format` and `make lint` as well. Use the pre-commit hook if you like.
- From the root of the project, run the tests via `make test`, and check out `make format-and-lint` as well. Use the pre-commit hook if you like.


When making a pull request, keep in mind the following (rough) guidelines:
Expand Down
1 change: 0 additions & 1 deletion matfree/backend/containers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"""Container types."""


from typing import NamedTuple # noqa: F401
1 change: 0 additions & 1 deletion matfree/backend/prng.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Pseudo-random-number utilities."""


import jax.random


Expand Down
1 change: 1 addition & 0 deletions matfree/backend/typing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Types."""

# fmt: off
from collections.abc import Callable # noqa: F401
from typing import Any, Generic, Iterable, Sequence, Tuple, TypeVar # noqa: F401, UP035
Expand Down
2 changes: 1 addition & 1 deletion matfree/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def baigolub96_logdet_spd(bound_spectrum, /, nrows, trace, norm_frobenius_squared):
"""Bound the log-determinant of a symmatric, positive definite matrix.
"""Bound the log-determinant of a symmetric, positive definite matrix.
This function implements Theorem 2 in the paper by Bai and Golub (1996).
Expand Down
5 changes: 1 addition & 4 deletions matfree/lanczos.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,7 @@ def _validate_unit_2_norm(v, /):
# but we can make it obvious that the result is unusable.
is_not_normalized = np.abs(linalg.vector_norm(v) - 1.0) > 10 * np.finfo_eps(v.dtype)
return control_flow.cond(
is_not_normalized,
lambda s: np.nan() * np.ones_like(s),
lambda s: s,
v,
is_not_normalized, lambda s: np.nan() * np.ones_like(s), lambda s: s, v
)


Expand Down
46 changes: 22 additions & 24 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ test =[
"pytest",
"pytest-cases",
]
lint =[
format-and-lint =[
"matfree[test]",
"pre-commit",
]
format =[
"isort",
"black",
"blackdoc",
]
doc = [
"mkdocs",
Expand All @@ -54,8 +51,7 @@ doc = [
full = [
"matfree[cpu]",
"matfree[test]",
"matfree[lint]",
"matfree[format]",
"matfree[format-and-lint]",
"matfree[doc]",
]

Expand All @@ -74,17 +70,18 @@ version_file = "matfree/_version.py"
"Issue tracker" = "https://github.com/pnkraemer/matfree/issues"



[tool.ruff]
include = ["*.py", "**/pyproject.toml", "*.ipynb"]
include = ["**.py", "**/pyproject.toml", "**.ipynb"]

[tool.ruff.lint]
# See: https://beta.ruff.rs/docs/rules/
select = [
# pycodestyle (warning, error)
"W",
"E",
# Pyflakes:
"F",
# pydocstyle:
"D",
# pyupgrade:
"UP",
# flake8-bugbear:
Expand Down Expand Up @@ -119,24 +116,25 @@ ignore = [
"D213",
# zip(..., strict=True/False) is not supported on Python < 3.10
"B905",
# Magic methods don't need a docstring:
"D105",
]

[tool.ruff.per-file-ignores]
# We don't write docstrings for the backend
"matfree/backend/*.py" = ["D103"]
[tool.ruff.format]
# Use `\n` line endings for all files
line-ending = "lf"
# Prefer single quotes over double quotes.
quote-style = "double"
skip-magic-trailing-comma = true

[tool.ruff.lint.isort]
split-on-trailing-comma = false


[tool.ruff.pydocstyle]
convention = "numpy"


[tool.isort]
multi_line_output = "3"
include_trailing_comma = "true"
force_grid_wrap = "0"
use_parentheses = "true"
line_length = "88"

[tool.black]
line-length = "88"
[tool.pytest.ini_options]
addopts = "-v"
testpaths = [
"tests"
]
1 change: 1 addition & 0 deletions scripts/generate_api_docs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Generate API docs from the source."""

import pathlib


Expand Down
1 change: 1 addition & 0 deletions scripts/tutorials_to_py_light.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Transform py:light scripts into Jupyter notebooks."""

import contextlib
import os
from re import sub
Expand Down
1 change: 1 addition & 0 deletions tests/test_hutchinson/test_frobeniusnorm_squared.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the estimation of squared Frobenius-norms."""

from matfree import hutchinson
from matfree.backend import func, linalg, np, prng, tree_util

Expand Down
1 change: 1 addition & 0 deletions tests/test_hutchinson/test_trace.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test trace estimation."""

from matfree import hutchinson
from matfree.backend import func, linalg, np, prng, tree_util

Expand Down
1 change: 1 addition & 0 deletions tests/test_hutchinson/test_trace_and_diagonal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test joint trace and diagonal estimation."""

from matfree import hutchinson
from matfree.backend import func, np, prng, tree_util

Expand Down
1 change: 1 addition & 0 deletions tests/test_lanczos/test_funm_vector_product.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test matrix-function-vector products via Lanczos' algorithm."""

from matfree import lanczos, test_util
from matfree.backend import linalg, np, prng

Expand Down
1 change: 0 additions & 1 deletion tests/test_lanczos/test_svd_approx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Tests for SVD functionality."""


from matfree import lanczos, test_util
from matfree.backend import linalg, np, testing

Expand Down
1 change: 1 addition & 0 deletions tests/test_pinv/test_pseudo_inverse_correct.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for pseudo-inverse functionality."""

from matfree import pinv
from matfree.backend import func, linalg, np, prng, testing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test matrix-polynomial-vector algorithms via Chebyshev's recursion."""

from matfree import polynomial, test_util
from matfree.backend import linalg, np, prng

Expand Down

0 comments on commit 24442bb

Please sign in to comment.