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 for linting/auto-formatting #508

Merged
merged 17 commits into from
Mar 21, 2023
Merged

Add pre-commit for linting/auto-formatting #508

merged 17 commits into from
Mar 21, 2023

Conversation

stefsmeets
Copy link
Contributor

@stefsmeets stefsmeets commented Mar 13, 2023

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 to isort. New checks can be enabled in pyproject.toml.

Running the linters now goes through pre-commit.

# staged files only
pre-commit

# all files
pre-commit run --all-files

You can enable automatic linting and code formatting with pre-commit on each commit:

pre-commit install

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:

git config --unset core.hooksPath

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

@stefsmeets stefsmeets marked this pull request as ready for review March 13, 2023 15:11
@stefsmeets
Copy link
Contributor Author

@cwmeijer Would you be willing to review this? Let me know what you think!

Copy link
Contributor

@cwmeijer cwmeijer left a 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?

dianna/__init__.py Show resolved Hide resolved
@stefsmeets
Copy link
Contributor Author

stefsmeets commented Mar 14, 2023

Yeah, good question!

So what happens is that pre-commit checks which files have changed (essentially git diff). Every file that is changed gets passed to some hooks based on the file extension. For example, it does not make sense to pass a yaml or markdown file to a python linter.

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 pylint, flake8, or ruff check), others also update and format the code (like ruff check --fix, yapf, isort). You will have to stage these again and if they pass without changes then the pre-commit hook is passed.

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.

@stefsmeets
Copy link
Contributor Author

I guess we can enable mypy here as a step towards #85

pyproject.toml Show resolved Hide resolved
@egpbos
Copy link
Member

egpbos commented Mar 14, 2023

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.

@cwmeijer
Copy link
Contributor

Sorry for the delay in approving. LGTM asap :-)

@stefsmeets
Copy link
Contributor Author

Cool, I will do a full re-format sometime next week.

Also a reminder to run:

git config --unset core.hooksPath

to remove the old hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

isort import error pylint configuration Code formatting and pre-commit hooks
3 participants