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

Question:

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.

Asked By: floatingpurr

||

Answers:

I would recommend keeping the linter stuff only in the config of pre-commit.

pre-commit doesn’t necessarily run as a pre-commit hook. You can run the checks every time by pre-commit run --all-files or if you want to run it only on given files with pre-commit run --files path/to/file.

You can even say which which check should run, e.g. pre-commit run black --all-files

Answered By: finswimmer

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:

  • Poetry for dependency management
  • local dev packages for your IDE (via Poetry)
  • pre-commit hooks

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

Answered By: floatingpurr