pip-tools icon indicating copy to clipboard operation
pip-tools copied to clipboard

pip-compile syncing changes to pre-commit config file

Open MRigal opened this issue 3 years ago • 4 comments

What's the problem this feature will solve?

One might have a dev_requirements file specifying linter for CI jobs that are also used by pre-commit hooks. It would be great if pip-compile could not only create/update the requirements.tx file, but also the .pre-commit-config.yaml file to not have a mismatch on black for example.

Describe the solution you'd like

  1. pip-compile would check if it finds some pre-commit config file, a path might be passed as extra args if preferred.
  2. It would extract the libraries used in pre-commit and their version
  3. It would check for the libraries to be found in the requirements.txt or setup.py and it will make sure that the version outcome is reflected in the pre-commit config file

For example, if the pre-commit config file has the following content:

repos:
-   repo: https://github.com/psf/black
    rev: 21.4b0
    hooks:
    - id: black

And dev_requirements.in the following:

black

Then we would have dev_requirements.txt with

black==21.6b0

And in .pre-commit-config.yaml

rev: 21.6b0

I'm not sure if this should be a plugin, but as pre-commit is so much used in pip-tools itself and is turning a de facto standard I think it could make sense to have directly integrated to pip-compile.

Should it be enabled by default? I would say yes, but it could be an option to be enabled, possibly by passing the config file path as argument.

Another possibility would be a pre-commit plugin, but I tend to think it should be added to pip-compile directly...

MRigal avatar Jun 24 '21 16:06 MRigal

@asottile maybe you have an opinion on this? If you think it is a nice addition and give me in indication how to add/integrate this ideally, I could try to work on this somewhen this summer...

MRigal avatar Jul 14 '21 14:07 MRigal

@MRigal @asottile From a quick glance I wonder if pre-commit's autoupdate feature could be a good starting point?

The idea would be to add a --from-requirements-file option which tries to install the version pinned by pip-tools.

anrie avatar Jul 14 '21 14:07 anrie

it's not that simple, notably yaml is currently not possible to machine-rewrite: https://github.com/pre-commit/pre-commit/issues/945#issuecomment-835889691

asottile avatar Jul 14 '21 16:07 asottile

This would be a really nice feature to have. So much so that I wrote my own pre-commit plugin piptools-sync. Meant to be a stop-gap before a more formal solution was found.

Stephen-RA-King avatar Jun 28 '23 22:06 Stephen-RA-King