-
Notifications
You must be signed in to change notification settings - Fork 13
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 for linting/auto-formatting #508
Conversation
@cwmeijer Would you be willing to review this? Let me know what you think! |
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.
Looks great!
I have to say that I don't fully understand what code is responsible for actually changing the code before a commit, but I'm sure you'll explain me soon ;-)
Could you have a look at my comment?
Yeah, good question! So what happens is that Each of the hooks then runs on the changed files. These can then change the code if they wish. This is no different than running each of the hooks individually on the command line. Some simply emit warnings (like All these hooks are defined in: https://github.com/dianna-ai/dianna/blob/61fe210edb8c95e69e2b54bc6ea85cc3eb220c8e/.pre-commit-config.yaml You'll notice there are some that check whether yaml files are valid (check-yaml), or whether the python code actually parses correcty (check-ast). Then there are the heavy lifters yapf and ruff which format and lint the code. I also tentatively added mypy, which we could enable at some point for type checking. |
I guess we can enable mypy here as a step towards #85 |
Super nice PR! Sorry for the unasked review, but I was just very interested to see how this would look in practice and in particular in DIANNA which I know well :) My only minor complaints are about disabling pylint complaints, as the examples above illustrate ;) I know there's always a trade-off with being realistic in terms of how much time you can spend on polishing. But DIANNA is growing into quite a big thing, so I would argue that in this case the strictest standards should be seriously considered. |
Sorry for the delay in approving. LGTM asap :-) |
Cool, I will do a full re-format sometime next week. Also a reminder to run:
to remove the old hook. |
This PR adds
pre-commit
as a tool for managing pre-commit hooks and code linting. It also replaces pylint by ruff as a linter. Ruff also sorts imports similar toisort
. New checks can be enabled inpyproject.toml
.Running the linters now goes through pre-commit.
You can enable automatic linting and code formatting with
pre-commit
on each commit:This replaces the git hook from before. The advantages are that this only checks changed files, issues are automatically fixed (if possibe/enabled), and it is much faster than prospector/pylint.
If you use the old hook, you will need to run:
You can also install ruff locally (
pip install ruff
) and run it on the entire codebase:ruff check dianna --fix
.I also added
yapf
as a code formatter. At some point we should run yapf on the entire code base so that everyone is up-to-date. This may hurt a bit with open PRs, but the reward is that we no longer have to worry about code formatting / fighting with pylint. I will create a new PR once this one is accepted.I realized I could not make this PR without fixing some standing issues to pass the linter, so I fixed some whitespace issues, added missing parameters in docstrings, and looked at all the pylint pragma's.
Closes #492
Closes #493
Closes #496