Improve your Django Code with pre-commit

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • ruff

    An extremely fast Python linter and code formatter, written in Rust.

  • A kind reddit user by the name of lanemik suggested I give ruff a go. I have heard of this tool before, but never actually gave it a go.

  • basic-django

  • Before we continue, I would like to mention that you can look at all the code that we wrote in this tutorial by looking into the pre-commit PR on the basic-django repo.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • autoflake

    Removes unused imports and unused variables as reported by pyflakes

  • Ruff can be used to replace Flake8 (plus dozens of plugins), isort, pydocstyle, yesqa, eradicate, pyupgrade, and autoflake, all while executing tens or hundreds of times faster than any individual tool.

  • pre-commit

    A framework for managing and maintaining multi-language pre-commit hooks.

  • I am going to go through each step with you, but I encourage you to check out the official website. The instructions they have are very useful.

  • pre-commit-hooks

    Some out-of-the-box hooks for pre-commit

  • exclude: .*migrations\/.* repos: - repo: https://github.com/pre-commit/pre-commit-hooks ... - repo: https://github.com/psf/black rev: 22.12.0 hooks: - id: black language_version: python3.9

  • black

    The uncompromising Python code formatter

  • Black is a Python code formatter. This means that when this hook will run, all the files that you want to commit will be checked for any inconsistencies and bad styling (based on PEP 8 standard). This hook will automatically fix those issues.

  • isort

    A Python utility / library to sort imports.

  • repos: ... pre-commmit stuff ... black stuff - repo: https://github.com/pycqa/isort rev: 5.12.0 hooks: - id: isort name: isort (python)

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • Flake8

    flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.

  • Pylint

    It's not just a linter that annoys you!

  • One last thing to do before running the hooks is to create a config file, just like we did with flake8. For this you are going to create a pylintrc file at the roor of your project and copy the contents of the pylintrc file from the pylint repo (here is the link to it).

  • djLint

    ✨ HTML Template Linter and Formatter. Django - Jinja - Nunjucks - Handlebars - GoLang

  • For Django users, which you presumably are, this will be a God send! Check out the repo. This is an HTML linter... but for Django templates 🤯

  • Poetry

    Python packaging and dependency management made easy

  • - repo: https://github.com/python-poetry/poetry rev: '1.4.1' hooks: - id: poetry-export args: [ "-f", "requirements.txt", "-o", "requirements.txt", "--without-hashes" ]

  • yesqa

    Automatically remove unnecessary `# noqa` comments

  • Ruff can be used to replace Flake8 (plus dozens of plugins), isort, pydocstyle, yesqa, eradicate, pyupgrade, and autoflake, all while executing tens or hundreds of times faster than any individual tool.

  • ruff-pre-commit

    A pre-commit hook for Ruff.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts