-
Notifications
You must be signed in to change notification settings - Fork 11
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
Proposal of expansion of anemoi-wide pre-commit hooks and ruff ruleset for code quality, security and maintainability #10
Comments
Just commenting to say I strongly support adding these (particularly type annotations and docstrings) - sounds great! 🥳 . I missed the technical subgroup meeting where pre-commit was discussed due to holiday. |
Thanks @leifdenby! I actually have an ammendment, we have experimentally switched on I would like to amend the proposal to:
with these specific ignores:
Since you noted annotations @leifdenby, here is my reasoning to use a subset: Annotations for Reasoning: I chose the "explicit add" route, which is quite verbose, but I think it's quite useful to see all the options. But I can be convinced that |
We also got the suggestion that adding Conventional Commits would be a great addition as a pre-commit hook.
We had been using those across some internal repos already anyways, so i would be in favour. |
IMO, one point to have in mind in configuring ruff is that we should keep refactoring easy and fast. This is especially true in the early stages of development where flexibility is crucial, but stays valid as long as the code is alive. As an example, requesting having a docstring for every function would not make sense and I don't think anybody wants this. |
And duck typing should be kept possible too. |
Is your feature request related to a problem? Please describe.
Maintainability and code quality are important parts to keep anemoi alive and well and avoid technical debt.
Pre-commit hooks are an easy way to automatically flag and possibly fix these issues, but above this we can run pre-commit in Github Actions to automatically check code compliance across many definitions.
We already implement many pre-commit hooks, but from experience I would propose the following hooks in addition:
Describe the solution you'd like
pygrep-hooks
to enforce type annotations, check for blanket noqa's (they should be specific to what they exempt) and check forlog.warns
docsig
to check docstrings against function signatureThere is no formal check that docstrings actually represent the function they describe. The additional settings make the docstring checking only necessary, when parameters are set and doesn't fail when no docstring is set, as a fairly lenient implementation. (This can be a point of discussion, whether we want to force docstrings and parameter descriptions.)
ruff
ruleset toALL
, which expands the checking to a wide variety of possible problems.By default
ruff
includes rulesetE
andF
which are pydocstyle errors and flake8 errors.There are many more rulesets that improve the overall code quality and should be considered. We could also enable those explicitly, or simply rely on
ALL
to include the full expansion of community accepted best practices.From experimentation I had also disabled some specific rulesets:
"E203", whitespace before punctuation
"D100", missing docstring in public module
"D101", missing docstring in public class
"D102", missing docstring in public method
"D103", missing docstring in public function
"D104", missing docstring in public package
"D105", missing docstring in magic method
"D401", First line of docstring written in imperative mood
"S101", Use of assert (asserts are usually good in our case)
"PT018", Composite pytest asserts
These could be discussed, as empty docstrings may actually not be wanted in the framework.
Describe alternatives you've considered
Alternatives would be to enable each package we want individually:
Currently in AIFS we are working with these:
Only disabling four, which would still be useful for a framework. The boolean traps ruleset might make coding harder however, and might necessitate some refactoring of parts of the code.
Alternatively we can introduce pre-commit hooks these rulesets implement:
bandit
for common code vulnerabilities (implemented in rulesetS
):docformatter
to enforce consistent formatting of docstrings (ruleset from pydocstyleD
,E
,W
):pyupgrade
to automatically upgrade Python syntax from older patterns (e.g. upgrading from percent style formatting'%s %s' % (a, b)
to'{} {}'.format(a, b)
)Additional context
Bandit or ruleset
S
especially may expose vulnerabilities in code, which makes our life as maintainers a little easier.Organisation
ECMWF
The text was updated successfully, but these errors were encountered: