-
Notifications
You must be signed in to change notification settings - Fork 9
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
Major Refactor: Add Ruff Linting, Nox, and Pre-Commit Hook #266
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #266 +/- ##
==========================================
- Coverage 88.60% 88.54% -0.07%
==========================================
Files 38 38
Lines 3405 3386 -19
==========================================
- Hits 3017 2998 -19
Misses 388 388 ☔ View full report in Codecov by Sentry. |
for more information, see https://pre-commit.ci
…n definitions in test validate metrics. Resolved E731 linting error
…ding action comments for ruff to disable linting rules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good! I'm pushing a couple of small changes, and then I'll go ahead and merge this.
Following up from my earlier comment: we've gone ahead and added pre-commit.ci.
Summary:
This PR refactors the project by adding Ruff Linting and formatting, integrated with Nox and an optional Pre-commit hook. It enhances both code quality and development efficiency.
Key Changes:
Next Steps & Open Questions:
Resolves issue #51
Configuration Details:
1. Ruff Linting Configuration:
The current Ruff configuration includes the most commonly used linting rules. (Future PRs will likely incorporate more linting rules, as discussed here).
E
): Enforces style consistency based on PEP8.F
): Detects basic errors such as undefined variables or missing imports.UP
): Ensures usage of modern Python syntax, making our code more future-proof.SIM
): Improves code readability by suggesting simplified expressions. We have chosen to ignore ruleSIM105
since our team prefers explicit boolean comparisons in some cases.I
): Ensures imports are organized and ordered consistently, improving code readability and maintainability.Note that a couple of linting rules were ignored with
#noqa <ruff rule>
(ignoring a single line) and#ruff: noqa <ruff rule>
(ignoring entire file):E501
for too long lines has been ignored several times to ensure for example that links will still work or flow charts remain readableI001
sorting imports has been ignored once insrc/plenoptic/__init__.py
as the import order matters to avoid circular dependencies and tests to failF401
unused imports# ruff: noqa: F401
is used in several__init__.py
files to ignore unused imports. When unused imports are removed, tests fail.SIM105
: Checks fortry
-except
-pass
blocks that can be replaced with thecontextlib.suppress
context manager.pyproject.toml
F403
flags wildcard imports*
, which is bad practice. (This is part of the issue Improve API #246 and notes have been added there)To remove wildcard (
*
) imports, the 2 recommended approaches are:__init__.py
File:from .validate import remove_grad
plenoptic.tools.validate
from . import validate
within the subpackage.validate.py
, define a list called__all__ = ["remove_grad", ...]
, which specifies what can be imported from the file.__init__.py
, and it controls the public API of that module.2. Ruff Formatting
Currently, formatting is enforced to emulate Black in
pyproject.toml
:Nox & Pre-commit Hooks and their Configurations:
TL;DR:
noxfile.py
.pip install pre-commit
. Pre-commit checks can be ignored, by adding--no-verify
to your commit message.2. Nox Configuration:
Nox is a Python-based task runner that is used to automate various development workflows, such as testing, building, and deploying applications. It allows you to define sessions, which are Python functions in the
noxfile.py
decorated with@nox.session
with various parameters, such asname
, by which these sessions can then be executed via the command line. This easily allows to test code by running pre-defined tasks with a single command across multiple Python versions without manually setting up environments.Example: Based on the following
noxfile.py
, the commandnox -s tests
would run the session named "tests” below.nox
without any commands would run all the sessions defined in thenoxfile.py
.Here are the current Nox sessions available for execution:
nox -s lint
nox -s tests
nox -s coverage
3. Pre-Commit Configurations:
Hook:
A pre-commit hook can be added to run Ruff linting on code before it is committed. This ensures that developers catch and fix linting issues early in the development process. It only runs when installed and can be bypassed with
git commit -m <my commit message> --no-verify
.Considered Configurations: (in
.pre-commit.yaml
:(Legend: ✅: included, ❓: recommended, but not included, possibly in future PR)
check-added-large-files
✅check-case-conflict
✅File.txt
andfile.txt
would be considered the samecheck-merge-conflict
✅<<<<<<<
,=======
,>>>>>>>
) in files.check-symlinks
❓check-yaml
✅debug-statements
✅print
,console.log
) left in code.end-of-file-fixer
✅trailing-whitespace
✅CI Pipeline:
Finally, the CI configuration
ci.yml
has been updated to include a step each for running Ruff formatting and linting check on all new commits and pull requests. Two separate actions were chosen to signal the developer which combination of the two might be causing an error.