Formatting

The sdr 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/sdr/_version.py"]
line-length = 120

[tool.ruff.lint]
select = [
    "E",  # pycodestyle
    "F",  # Pyflakes
    "UP", # pyupgrade
    "B",  # flake8-bugbear
    # "SIM",# flake8-simplify
    "I", # isort
]
ignore = ["E501", "E713", "E714"]

[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.1.4
  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: Nov 04, 2023