Essential Python tools for writing quality code

Essential Python tools for writing quality code

Writing quality code is essential for both personal and team projects. It helps to reduce the number of bugs, makes the code more readable and maintainable, and improves the overall quality of the project.

In this article some basic libraries that I use in everyday life.

  • flake8: A tool that checks your code for PEP 8 compliance and detects syntax errors, unused imports, and undefined variables. I use setup.cfg to increase line length from 80 to 120.
[flake8]
max-line-length: 120
  • mypy: A static type checker that helps you find common errors such as type mismatches and incorrect function calls before you run the code.

  • black: A code formatter that automatically formats your code to be PEP 8 compliant. It allows to focus more on content. In a team, it simplifies code review, because all members follow the same code style.  For line length, there is an option --line-length=120

  • isort: A tool that sorts your imports alphabetically and separates them into sections. The reasons are the same as for black.

  • bandit: A security linter that checks your code for common security issues such as hardcoded passwords and SQL injection vulnerabilities. In the latest version, it also checks the timeouts for requests. It can block your app for an infinite time if they are not set.

  • commitlint: A not python tool that checks your commit messages for compliance with conventional commit standards. It allows you to have a more readable commit history and use it with automated tools, for example, to generate a changelog.

For all libs, there is native support or plugins for VSCode or PyCharm, but it is useful to force them with pre-commit too. To adopt pre-commit into your system, you simply need to perform the following actions:

  • Install pre-commit: pip install pre-commit
  • Add pre-commit to requirements.txt (or requirements-dev.txt).
  • Define .pre-commit-config.yaml with the hooks you want to include.
repos:
  - repo: https://github.com/PyCQA/flake8
    rev: 7.0.0
    hooks:
      - id: flake8
        stages: [commit]
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.8.0
    hooks:
      - id: mypy
        stages: [commit]
  - repo: https://github.com/psf/black
    rev: 24.1.1
    hooks:
      - id: black
        args: [--line-length=120]
        stages: [commit]
  - repo: https://github.com/PyCQA/isort
    rev: 5.13.2
    hooks:
      - id: isort
        stages: [commit]
  - repo: https://github.com/PyCQA/bandit
    rev: 1.7.7
    hooks:
      - id: bandit
        stages: [commit]
  - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
    rev: v9.11.0
    hooks:
      - id: commitlint
        additional_dependencies: ['@commitlint/config-conventional']
        stages: [commit-msg]
  • Create commitlint config echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

  • Execute pre-commit install -t pre-commit -t commit-msg to install git hooks in your .git/ directory.

comments powered by Disqus