Recently I’ve been trying to improve code quality on my scripts, and I have found some tools that help me towards that goal.
Since projects are now stored in the file system, we can run all of these tools under the
Unfortunately these don’t work for scripts inside components.
- Python 3.9
- Optional packages
On my personal blog I have described on two separate posts how I use all of these tools (here, and here), but instead of just reposting I wanted to share with the community how I use it for Ignition projects for work and on a scripting project called incendium which I’ve shared on Ignition Exchange and its source code at GitHub.
I won’t bore you with details on what is code quality, and why it matters, as many people are more qualified, but one interesting article I’ve read is Python Code Quality: Tools & Best Practices, instead I’ll share with you how I use all of these tools.
So all lines of code are kept to a maximum of 79 characters, and 72 characters for docstrings and comments.
If you’d like to know more on the options available for each command, you may find them by running
<command> --help or by reading their respective documentation. I’ll limit to only posting the commands as I use them.
$ black --line-length 79 --target-version py27 .
$ isort --multi-line 3 --profile black --python-version 27 .
$ flake8 --max-complexity=10 --max-doc-length=72 --ignore=F821,W503
$ pydocstyle --convention=google .
[flake8] ignore = F821, W503 max-complexity = 10 max-doc-length = 72
If you are committing your code to source control, I highly recommend using
pre-commit. And as its creator said, the order of the tools should be as follows:
- Code formatter (
- Imports sorter (
- Checker\Linter (
repos: - repo: https://github.com/psf/black rev: 21.7b0 hooks: - id: black - repo: https://github.com/PyCQA/isort rev: 5.9.3 hooks: - id: isort - repo: https://github.com/PyCQA/flake8 rev: 3.9.2 hooks: - id: flake8 - repo: https://github.com/PyCQA/pydocstyle rev: 6.1.1 hooks: - id: pydocstyle additional_dependencies: - toml
[tool.black] line-length = 79 target-version = ["py27"] [tool.isort] profile = "black" multi_line_output = 3 py_version = 27 [tool.pydocstyle] convention = "google"
It’s not just a linter that annoys you!
Pylint is a Python static code analysis tool which looks for programming errors, helps enforcing a coding standard, sniffs for code smells and offers simple refactoring suggestions.
While I don’t use
pre-commit, I do use it for detecting “code smells” and “bad practices”. that’s why I listed it as an optional package to install.
And this is the command I use for checking my code:
$ pylint --disable=C0411,E0401,E0602,E1101,R0205,R1725,W0235,W0622,W0707 <dir>
A better option would be to generate a
pylintrc and modify as desired.
$ pylint --generate-rcfile > pylintrc
By using these tools (or a subset of them) you might end up with readable code that would be easy for others to understand, enhance, and maintain.
pre-commit ci is a continuous integration service for the pre-commit framework. Currently free for open source projects, but it also offers plans for private repos. Currently it only works on GitHub repos.