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

Conversation

weiji14
Copy link
Contributor

@weiji14 weiji14 commented Nov 10, 2023

What I am changing

  • Add pre-commit hooks with ruff linter/formatter to enforce certain rules on Python scripts and Jupyter notebooks.

How I did it

  • Add a .pre-commit-config.yaml file for standard pre-commit hooks
  • Add pyproject.toml configuration file for ruff formatter/linter rules
  • Configure pre-commit-ci GitHub App

How you can test it

  • Install pre-commit and run pre-commit run --all-files locally

Related Issues

References:

Adding a .pre-commit-config.yaml file with some pre-commit hooks and the ruff linter/formatter. The ruff linter is configured to do autofix, and will run on python scripts and jupyter notebooks.
Enforce certain ruff formatting and lint rules such as UNIX-style line-endings, pycodestyle, pyflakes, isort, numpy, pylint and pyupgrade.
@weiji14 weiji14 added the maintenance Boring but important stuff for the core devs label Nov 10, 2023
@weiji14 weiji14 self-assigned this Nov 10, 2023
weiji14 and others added 3 commits November 10, 2023 17:05
Fix `F841: Local variable `cli` is assigned to but never used` on trainer.py.
Setting up pre-commit.ci to only run updates quarterly instead of the weekly default. Also explicitly stating that Pull Requests will be autofixed.
Comment on lines +5 to +15
[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
]
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.

Comment on lines +20 to +23
# https://pre-commit.ci/#configuration
ci:
autofix_prs: true
autoupdate_schedule: quarterly
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

@@ -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!

@weiji14 weiji14 requested a review from a team November 10, 2023 04:23
Copy link
Member

@yellowcap yellowcap left a comment

Choose a reason for hiding this comment

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

Nice, I haven't used pre-commit.ci before. Seems handy!

Comment on lines +5 to +15
[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
]
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.

@weiji14 weiji14 marked this pull request as ready for review November 10, 2023 09:46
@weiji14
Copy link
Contributor Author

weiji14 commented Nov 10, 2023

Nice, I haven't used pre-commit.ci before. Seems handy!

Yeah, this is actually my first time setting it up too, but I've been meaning to try it for a while (it's only free for public repos).

Gonna merge this in now, thanks @yellowcap for reviewing!

@weiji14 weiji14 merged commit 3412060 into main Nov 10, 2023
2 checks passed
@weiji14 weiji14 deleted the pre-commit branch November 10, 2023 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants