Closed Vanuan closed 4 years ago
When looo started this repository I wanted to open a FEP like this one but I never did because of priorities elsewhere, so I'm happy it is being proposed now.
Related threads:
Thanks, made the announcement at these topics too.
Moved to wiki. Let's do discussions on the forum
Abstract
This is a proposal to enforce PEP8 rules gradually, rule by rule, workbench by workbench using the flake8 tool.
This proposal focuses primarily on fixing the following styling errors:
But the approach could be extended to add more PEP8 rules.
Motivation
When reviewing Python code it's important that the coding style is consistent. If it's inconsistent, reviewer spend time teaching contributors about the coding style. Contributors, OTOH are frustrated that there's an apparent discretion that reviewers apply when reviewing the code. Different workbenches have different rules, e.g. Path/CAM, so even reviewers can't always agree on the rules a specific codebase should follow. This takes unneeded discussions, effort and energy that would've better be spent elsewhere.
The problem above means there should be some objective set of rules enforced by an automated system. Thankfully, Python community already though about these issues and prepared PEP8 - a style guide for Python code and a corresponding tool to enforce them - Flake8.
Specification
The rules are described pretty well in both the original PEP8 document and in the dedicated packages: flake8-import-order and pep8-naming.
But let's duplicate those:
Imports
Naming
Rationale
Flake8 is a de-facto standard for style guide verification, so that choice is quite straightforward.
Alternatives
No alternatives I'm aware of. There's a codespell tool but that focuses on the proper use of English (automating spelling error correction).
Backwards Compatibility
To preserve compatibility, when renaming we should add a proxy name and mark it as deprecated and put a version in which it was deprecated. After a new version of FreeCAD is released, the deprecated name may be safely removed.
To make flake8 happy, we add the following comment:
Where
N802
is a name of an error we wish to ignore (until a new version of FreeCAD is released after which we can remove the ignore).Sample Implementation
First, install the packages:
python3 -m pip install flake8 flake8-import-order pep8-naming flake8-pyi
Add the
.flake8
config file at the root of the project:And run this command:
flake8
.By changing the
filename
andselect
options we can configure the scope of the rule enforcement and improve the enforcement coverage file by file, rule by rule, workbench by workbench.FreeCAD already uses Travis CI to run C++ tests. Having an additional step of Python-specific tests should not make it slower as it takes several seconds rather than minutes. If TravisCI is an issue, we can try CircleCI or other tools that give some free quota to improve the running time.
Related forum threads and wiki pages:
FAQ
Q: Is it compatible with rules we have in XXX workbench? A: The goal of this proposal is to enforce the PEP-8 rules we choose across all the workbenches. As long as these rules are supported by flake8, yes
Q: What if I don't like the rule applied in my specific case? A: You can add the line that violates the rule to ignore it (
# noqa: XXX
) or you could disable it in the whole file. Let's discuss on a case by case basis.Q: But this hurts my creativity, I can't express myself in my unique style! A: FreeCAD is a collaborative project, so it's important that the coding style is consistent even if some contributors don't like it. You read the code much more often than you write it, so it takes some time to get used to. Moreover, once you learn this coding style, you can contribute to any Python project as PEP8 rules are a de-facto standard in the Python community.
Q: But can this be not enforced by the tool? I promise that I won't violate any rules! A: Unfortunately, people make mistakes. Even in a good faith, contributors can't be trusted. Tools are objective and even if they fail, they fail consistently, so issues can be fixed in bulk.
Copyright
All FEPs are explicitly CC0 1.0 Universal.