Managing multiple Python lint tools with tox
Python has a sprawl of linting and style-check tools, each with a different purpose and its own configuration, which gets tedious to manage. I pulled them together with a tool called tox.
Lint tools used
requirements-dev.txt
1
2
3
4
5
6
7pytest==6.2.5
tox==4.26.0
black==25.1.0
bandit==1.8.3
flake8==7.2.0
mypy==1.16.0
isort==6.0.1tool explain pytest A test runner for Python. It automatically discovers and runs test functions. tox A tool for automating tests and checks across multiple environments. Used for CI/CD and supporting multiple Python versions. black A code formatter (auto-formatter). It automatically rewrites code to a PEP8-compliant style. bandit A security checker for Python code. It detects dangerous code patterns. flake8 A syntax and style checker for Python code. It reports PEP8 violations and the like. mypy A type checker for Python code. It verifies that type hints are used correctly. isort A tool that automatically organizes and sorts import statements. Often used together with black.
Example tox.ini configuration
This is a tox.ini that accounts for excluded directories with a CDK project in mind.
It’s reusable as-is.
1 | [tox] |
Usage
Running tests
1 | tox |
Running lint checks
1 | tox -e lint |
Auto-fixing lint findings
1 | tox -e lint_auto_fix |
GitHub Actions configuration
1 | name: CDK Unit Test |
Example in practice
I used this in my Raspberry Pi setup.
https://github.com/kenzo0107/raspi-talk/commits/main/tox.ini
That’s it.
I hope this helps.
Managing multiple Python lint tools with tox