pythonpre-commit-hookpython-poetrypre-commit.com

How to have a single source of truth for poetry and pre-commit package version?


I'm looking into this Python project template. They use poetry to define dev dependencies

[tool.poetry.dev-dependencies]
black = {version = "*", allow-prereleases = true}
flake8 = "*"
isort = "^5.6"
mypy = ">0.900,<1"
...

They use also pre-commit to check housekeeping things (e.g., formatting, linting, issorting, ...), both for git and for CI workflows:

minimum_pre_commit_version: 2.8.0
default_stages: [commit, push, manual]
repos:
  - repo: https://github.com/psf/black
    rev: 21.11b1
    hooks:
      - id: black
  - repo: https://github.com/pycqa/flake8
    rev: 4.0.1
    hooks:
      - id: flake8
        args: [--max-line-length=88]
  - repo: https://github.com/pycqa/isort
    rev: 5.10.1
    hooks:
      - id: isort
        args: [--filter-files]
  - ...

In my case, I definitely want a local version of dev packages managed by poetry for my IDE, and I'd like also to harness the pre-commit framework "as is", without switching to language: system. Working this way, I need to manage each package version in two different places.

Is there a non-manual way to keep dev packages versions (i.e,. black, flake8, isort, mypy, ...) aligned to a single source of truth? A Coockiecutter template could be an option, but it looks overkilling.


Solution

  • Finally, I created a pre-commit hook to do the job: https://github.com/floatingpurr/sync_with_poetry

    Edit: If you use PDM, you can refer to this one: https://github.com/floatingpurr/sync_with_pdm

    This hook just keeps in sync the repos rev in .pre-commit-config.yaml with the packages version locked into poetry.lock.

    If you use:

    this meta-hook can be useful to (un-)bump repos rev for you.