Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit hooks with ruff formatter/linter rules #26

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
hooks:
- id: ruff # Run the linter
args: [ --fix ]
types_or: [ python, pyi, jupyter ]
- id: ruff-format # Run the formatter
types_or: [ python, pyi, jupyter ]

# https://pre-commit.ci/#configuration
ci:
autofix_prs: true
autoupdate_schedule: quarterly
Comment on lines +20 to +23
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have set-up pre-commit.ci GitHub App to fix lint/formatting errors in Pull Requests by default, but we can disable this if it gets annoying.

Also, pre-commit autoupdate will be ran quarterly (default is weekly, but that might get noisy).

Extra configs can be found at https://pre-commit.ci/#configuration

15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.ruff.format]
# https://docs.astral.sh/ruff/settings/#format
line-ending = "lf" # Use UNIX `\n` line endings for all files

[tool.ruff.lint]
# https://docs.astral.sh/ruff/rules/
select = [
"E", # pycodestyle errors
"F", # pyflakes
"I", # isort
"NPY", # numpy
"PL", # pylint
"UP", # pyupgrade
"W", # pycodestyle warnings
]
Comment on lines +5 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if these rules from https://docs.astral.sh/ruff/rules are too much or too little!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me for now. We can always adapt if we want more or someone feels slowed down by any of the linters.

2 changes: 1 addition & 1 deletion trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
- https://lightning.ai/docs/pytorch/2.1.0/cli/lightning_cli.html
- https://pytorch-lightning.medium.com/introducing-lightningcli-v2-supercharge-your-training-c070d43c7dd6
"""
import torch
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See example commit 012a7a8 where pre-commit-ci automatically removed this unused import!

from lightning.pytorch.cli import ArgsType, LightningCLI

from src.datamodule import BaseDataModule
Expand All @@ -34,6 +33,7 @@ def cli_main(
trainer_defaults=trainer_defaults,
args=args,
)
return cli


# %%
Expand Down