Formatting

The galois library uses Ruff for static analysis, linting, and code formatting.

Install

First, ruff needs to be installed on your system. Easily install it by installing the development dependencies.

$ python3 -m pip install -r requirements-dev.txt

Configuration

The ruff configuration is provided in pyproject.toml.

pyproject.toml
[tool.ruff]
src = ["src"]
extend-include = ["*.ipynb"]
extend-exclude = ["build", "dist", "docs", "src/galois/_version.py"]
line-length = 120

[tool.ruff.lint]
select = [
    "E",  # pycodestyle errors
    "W",  # pycodestyle warnings
    "E",  # pycodestyle
    "F",  # Pyflakes
    "UP", # pyupgrade
    "B",  # flake8-bugbear
    # "SIM",# flake8-simplify
    "DTZ", # flake8-datetimez
    "I",   # isort
    "PL",  # pylint
]
ignore = [
    "E501",    # line-too-long
    "E713",    # not-in-test
    "E714",    # not-is-test
    "E741",    # ambiguous-variable-name
    "PLR0911", # too-many-return-statements
    "PLR0912", # too-many-branches
    "PLR0913", # too-many-arguments
    "PLR0915", # too-many-statements
    "PLR2004", # magic-value-comparison
    "PLR5501", # collapsible-else-if
    "PLW0603", # global-statement
    "PLW2901", # redefined-loop-name
    "UP006",   # non-pep585-annotation, type[FieldArray] renders wrong in docs
]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401", "F403"]

Run the linter

Run the Ruff linter manually from the command line.

$ python3 -m ruff check .

Run the formatter

Run the Ruff formatter manually from the command line.

$ python3 -m ruff format --check .

Pre-commit

A pre-commit configuration file with various hooks is provided in .pre-commit-config.yaml.

.pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v2.3.0
  hooks:
  - id: check-added-large-files
  - id: check-yaml
  - id: check-toml
  - id: end-of-file-fixer
  - id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
  rev: v0.4.7
  hooks:
  - id: ruff
  - id: ruff-format

Enable pre-commit by installing the pre-commit hooks.

$ pre-commit install

Run pre-commit on all files.

$ pre-commit run --all-files

Disable pre-commit by uninstalling the pre-commit hooks.

$ pre-commit uninstall

Run from VS Code

Install the Ruff extension for VS Code. Included is a VS Code configuration file .vscode/settings.json. VS Code will run the linter and formatter as you view and edit files.


Last update: Sep 05, 2024