Pre-commit hooks and black code formatting #224
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi Team,
Another one from me...maybe this one is controversial
I've added some standard pre-commit hooks to SHARPy in order to ensure code consistency and formatting, such that code reviews can be spent reviewing code and not formatting!
What are pre-commit checks? These are checks that run every time you commit your code and ensure that the formatting adheres to standard practice.
Black is a formatter that will modify the
.py
files to ensure the format is consistent (i.e. line length, end of file etc.). Don't worry, it won't break the code as it checks that the python byte-content is the same before and after the operation.As you can see, every single file in the repository has been modified to ensure consistent formatting. To avoid issues with
git blame
there is a new.git-blame-revs-ignore
file that contains these massive commits so that the history of the file is unobstructed by these changes.What is controversial? Black uses always double quotes for strings
"
rather than'
so every string in the code has been modified to this practice. This can be of course reverted.Black is only one stage of the process that formats the code. The next step would be using flake8 that actually checks for various things before allowing a commit. These checks can be very strict (preventing commits with unused variables, unused import statements, etc.) which the tool cannot change automatically since it is modifying code. Therefore, when I tried using
flake8
a lot of modifications are needed in the code files for the checks to pass which could be a project on its own making the code compliant. It could be worth it in the long run though, we could start with many exclusions (errors you don't check for) and adding them step by step.In any case let me know what you think about including these in SHARPy, additionally we'd just need to add a line to the CI pipeline to run these alongside the tests.
I have manually added
pre-commit
to the new environment file only. Not sure if you are looking to discard the others.