Introduce linter to enforce consistent coding conventions
Current Situation
Currently, there are no consistent coding conventions for contributions to this codebase. As the project is open source, inconsistencies and with that reduced code readability may be introduced by people following different coding guidelines.
Examples for inconsistencies are:
- Blank lines after docstrings: https://github.com/eclipse-basyx/basyx-python-sdk/blob/a6a904af37f9cccddd1d31f6b2f1921bfded4256/sdk/basyx/aas/model/submodel.py#L573-L576 https://github.com/eclipse-basyx/basyx-python-sdk/blob/a6a904af37f9cccddd1d31f6b2f1921bfded4256/sdk/basyx/aas/model/submodel.py#L622-L624 I am not sure how to handle this, as it is a well-discussed topic.
- Missing second blank line: https://github.com/eclipse-basyx/basyx-python-sdk/blob/a6a904af37f9cccddd1d31f6b2f1921bfded4256/etc/scripts/check_python_versions_supported.py#L49-L51
- Whitespaces (around operators): https://github.com/eclipse-basyx/basyx-python-sdk/blob/a6a904af37f9cccddd1d31f6b2f1921bfded4256/sdk/basyx/aas/model/datatypes.py#L587
- Unused imports: https://github.com/eclipse-basyx/basyx-python-sdk/blob/a6a904af37f9cccddd1d31f6b2f1921bfded4256/sdk/basyx/aas/backend/local_file.py#L14
- Unsorted import blocks: https://github.com/eclipse-basyx/basyx-python-sdk/blob/a6a904af37f9cccddd1d31f6b2f1921bfded4256/sdk/basyx/aas/model/datatypes.py#L26-L32
- Line is too long: https://github.com/eclipse-basyx/basyx-python-sdk/blob/a6a904af37f9cccddd1d31f6b2f1921bfded4256/etc/scripts/check_python_versions_supported.py#L17
- ...
Proposed Change
To avoid inconsistencies and style violations in both existing and future code, I suggest adding a linter. This linter could be used for pre-commit checks, as well as for CI checks. Since we already use mypy for type checking, adding a linter would be the next logical step. If we wanted to take it a step further, we could also check for code smells.
I have researched the following linter options:
I highly encourage the introduction of a linting process into the CI, as the absence of a compiler in Python raises the risk of runtime errors and puts additional responsibility on programmers. A linter can help us to ensure high code quality with less effort and helps new contributors to adapt to the project style.
Research Result
After researching the suggested linter options and evaluating based on functionality, flexibility and efficiency, I clearly favor Ruff as it offers a vast amount of rules, including those enforced by Pylint and many Flake8 plugins. It also ships with a formatter that follows the same layout as Black.
Suggested Rules
I searched the 900 available rules and selected a subset from which, in my opinion, the project would benefit. All rules are described by 1-3 letters followed by 1-3 digits. Omitting the digits includes all rules that start with the given letter + digit combination.
| Rule(s) | Short description | Remark |
|---|---|---|
E |
pycodestyle errors - already used today (includes max line length & operator whitespacing) |
|
W |
pycodestyle warnings - already used today |
exclude W505: max line length for docstrings |
I |
sorts, groups and formats imports by type (import ..., from ... import ...) |
|
N |
enforce PEP8 naming conventions |
|
F4 |
detect unused or covered imports and problems with __future__ |
exclude F403 and F405 for now due to high usage |
F5 |
check format strings (syntax, correct arguments) | |
F7 |
prevent return, break, continue, etc. outside function or loop rp. |
|
F8 |
fail on undefined or unused variables | |
RUF100 |
detect non necessary #noqa comments |
|
COM819 |
prevent trailing commas | |
B |
collection of patterns that often cause bugs ( except twice with same type, * unpacking after named argument, etc.) |
exclude B9 as the authors call it themselves controversial |
D204, D211, D201, D202, D300 |
enforce basic docstring formatting (use """, no blank lines for methods, one blank line after for classes) |
Highly customizable! |
A run ruff check with this ruleset on the current state of the develop branch delivers the following output:
86 I001 [*] unsorted-imports
43 F401 [*] unused-import
27 D202 [*] blank-line-after-function
23 F841 [-] unused-variable
20 N806 [ ] non-lowercase-variable-in-function
9 F541 [*] f-string-missing-placeholders
9 RUF100 [*] unused-noqa
8 B007 [ ] unused-loop-control-variable
6 COM819 [*] prohibited-trailing-comma
5 F811 [ ] redefined-while-unused
4 D204 [*] incorrect-blank-line-after-class
3 E226 [*] missing-whitespace-around-arithmetic-operator
2 B005 [ ] strip-with-multi-characters
2 E302 [*] blank-lines-top-level
2 N802 [ ] invalid-function-name
1 B006 [ ] mutable-argument-default
1 B023 [ ] function-uses-loop-variable
1 B026 [ ] star-arg-unpacking-after-keyword-arg
1 D201 [*] blank-line-before-function
1 D211 [*] blank-line-before-class
1 E241 [*] multiple-spaces-after-comma
1 E305 [*] blank-lines-after-function-or-class
1 N818 [ ] error-suffix-on-exception-name
Found 257 errors.
[*] 194 fixable with the `--fix` option (31 hidden fixes can be enabled with the `--unsafe-fixes` option).
In the future additional rules could be added to this ruleset, e.g. to require type annotations (via ANN) or docstrings (via D), to control the replacement of commonly used deprecated modules and functions (via UP) or to prevent bad coding style, like spaghetti code (via PLR).
We should discuss with wich ruleset we want to start and if we want to use the Ruff formatter in addition to the linter.
Using a formatter serves us the benefits of no more whitespace changes in commits and no discussions about formatting.
On the downside, the Ruff formatter is almost not customizable. We have to decide in common if we like the produced
formatting and if the benefits make this up.
I agree; I would also tend to use Ruff as I have experimented with it before. It's very easy to add different rule sets and customise the linter within the given framework. I haven't figured out how to write your own rules tho, and it's possible that this feature isn't even supported yet. However, Isort (to sort the imports), other linters and an extensive set of rules are already integrated.
Okay, this seems like the reasonable and modern choice. I say, immediately after the next release, the next task will be to include this ruff config into our CI pipeline and fix the errors it finds.
We of course need to make sure it doesn't change names of externally used classes or variables, as to not introduce breaking changes (which makes me wonder if there's the option to exclude a particular line from ruff, like with # type: ignore).
As soon as this is fixed, we release a minor patch with the refactored code.