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

[tool.ruff.lint]
exclude = ["docs/*"]
select = [
    "E",  # pycodestyle errors
    "W",  # pycodestyle warnings
    "F",  # pyflakes
    "UP", # pyupgrade
    "B",  # flake8-bugbear
    # "SIM", # flake8-simplify
    "DTZ", # flake8-datetimez
    "D",   # pydocstyle
    "I",   # isort
    "PL",  # pylint
    "NPY", # numpy rules
    # "RUF", # ruff rules
    "PERF", # perflint
]
ignore = [
    "D200",    # fits-on-one-line
    "D205",    # blank-line-after-summary
    "D212",    # multi-line-summary-first-line
    "E501",    # line-too-long
    "E713",    # not-in-test
    "E714",    # not-is-test
    "PLR2004", # magic-value-comparison
    "PLR0913", # too-many-arguments
    "PLR5501", # collapsible-else-if
    "PLR0912", # too-many-branches
    "PLR0915", # too-many-statements
    "PLW0603", # global-statement
    "UP006",   # non-pep585-annotation, type[FieldArray] renders wrong in docs
]
extend-select = [
    "D213", # multi-line-summary-second-line
]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401", "F403"]
"tests/*" = ["D", "D415"]

[tool.ruff.lint.pydocstyle]
convention = "google"

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
    args: ['--maxkb=1000']
  - 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.5.1
  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 06, 2024