Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit hook for pip-compile #976

Merged
merged 8 commits into from
Mar 19, 2020

Conversation

atugushev
Copy link
Member

@atugushev atugushev commented Oct 30, 2019

Resolves #974.
Refs #882.

Changelog-friendly one-liner: Add pre-commit hook for pip-compile.

Contributor checklist
  • Provided the tests for the changes.
  • Gave a clear one-line description in the PR (that the maintainers can add to CHANGELOG.md on release).
  • Assign the PR to an existing or new milestone for the target version (following Semantic Versioning).

description: Automatically compile requirements.
entry: pip-compile
language: python
files: (requirements\.in|setup\.py)$
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest pyproject.toml and setup.cfg as well for completeness

and probably some docs on how to use this / when you might want to use this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest pyproject.toml and setup.cfg as well for completeness

Thanks for noticing! I've missed that out. :(

and probably some docs on how to use this / when you might want to use this

Could be enough something like https://github.com/asottile/flake8-walrus#as-a-pre-commit-hook?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep! that one's a little less standard, here's the copy I usually use: https://github.com/asottile/add-trailing-comma/#as-a-pre-commit-hook

Copy link
Member Author

@atugushev atugushev Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May we borrow it? :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure! you may want to suggest stages: [pre-push] as well as pre-commit install --hook-type pre-push as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up to you, not my project :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 1bff1e9 and e33f07d.

Copy link
Member Author

@atugushev atugushev Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried this hook as a user on my repositories and it looks pass_filenames: false (and probably files too) needs to be removed. Otherwise, pre-commit will run something like:

$ pip-compile setup.py setup.cfg

which is totally wrong, since setup.cfg is not a requirement file.

I'd let users configure files and args manually for their special case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there's no good default, it might make sense to set files: ^$ + pass_filenames: false and document that the consumer must configure args: [requirements.in] + files: ^(...)$ (for example)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 5f2dea3

@atugushev atugushev added the enhancement Improvements to functionality label Oct 30, 2019
atugushev and others added 2 commits October 30, 2019 22:59
Add pyproject.toml and setup.cfg as well for completeness.

Co-Authored-By: Anthony Sottile <asottile@umich.edu>
Add a basic doc on how to use pip-compile as pre-commit hook.

Co-Authored-By: Anthony Sottile <asottile@umich.edu>
@codecov
Copy link

codecov bot commented Oct 30, 2019

Codecov Report

Merging #976 into master will increase coverage by 0.16%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #976      +/-   ##
==========================================
+ Coverage   99.26%   99.42%   +0.16%     
==========================================
  Files          34       34              
  Lines        2446     2443       -3     
  Branches      306      302       -4     
==========================================
+ Hits         2428     2429       +1     
+ Misses         10        8       -2     
+ Partials        8        6       -2     
Impacted Files Coverage Δ
piptools/sync.py 100.00% <0.00%> (ø)
piptools/utils.py 100.00% <0.00%> (ø)
tests/conftest.py 100.00% <0.00%> (ø)
tests/test_sync.py 100.00% <0.00%> (ø)
tests/test_cache.py 100.00% <0.00%> (ø)
tests/test_utils.py 100.00% <0.00%> (ø)
piptools/resolver.py 100.00% <0.00%> (ø)
tests/test_writer.py 100.00% <0.00%> (ø)
piptools/locations.py 100.00% <0.00%> (ø)
tests/test_cli_sync.py 100.00% <0.00%> (ø)
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d405bf9...ae121d1. Read the comment docs.

@dirn
Copy link

dirn commented Feb 11, 2020

@atugushev does the hook fail for you? I tried using it but, while I successfully get a new requirements.txt file, the pre-commit shows the hook as passing.

@atugushev
Copy link
Member Author

atugushev commented Feb 11, 2020

@dirn

does the hook fail for you? I tried using it but, while I successfully get a new requirements.txt file, the pre-commit shows the hook as passing.

I've updated the hook. Could you try it?

@dirn
Copy link

dirn commented Feb 11, 2020

I think I understand my problem a bit better now. The hook passes if requirements.txt doesn't already exist. Once it does, the hook correctly fails. I'm not sure there's much pip-compile can do about that.

@atugushev
Copy link
Member Author

@dirn Yeah, this works if requirements.in and requirements.txt are in VCS.

- don't pass filenames since it would break CLI
- update files filter
@sfdye
Copy link
Member

sfdye commented Mar 18, 2020

I tried in my project with the this

config
repos:
  - repo: https://github.com/atugushev/pip-tools
    rev: pre-commit-hook
    hooks:
      - id: pip-compile
$ pre-commit run
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /Users/xxx/.cache/pre-commit/patchxxx.
pip-compile..............................................................Passed
[INFO] Restored changes from /Users/lwan/.cache/pre-commit/patchxxx.

It worked from a pip-compile perspective, but unable to detect any files other than requirements.(in|txt). (e.g. requirements/main.txt)

It will be nice to support any requirement file in any location.

@atugushev
Copy link
Member Author

@sfdye

Thank you for checking this out. Default pre-commit config handles only default values. If you have other than default values, you should configure args and files correspondingly. For example:

repos:
  - repo: https://github.com/atugushev/pip-tools
    rev: pre-commit-hook
    hooks:
      - id: pip-compile
        files: ^requirements/main\.(in|txt)$
        args: [requirements/main.in]

Copy link
Member

@sfdye sfdye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@atugushev
Copy link
Member Author

@sfdye

Since you've had an issue with this, it seems I should definitely address this #976 (comment).

@sfdye
Copy link
Member

sfdye commented Mar 18, 2020

Yes, please. Would love to see this merged😁

@atugushev
Copy link
Member Author

atugushev commented Mar 18, 2020

@sfdye

Great! Please, review the latest changes if you get a chance.

@atugushev atugushev added this to the 5.0.0 milestone Mar 18, 2020
README.rst Show resolved Hide resolved
@atugushev atugushev merged commit f4a4f7b into jazzband:master Mar 19, 2020
@atugushev atugushev deleted the pre-commit-hook branch March 19, 2020 05:22
@atugushev
Copy link
Member Author

@sfdye thanks for reviewing this! 🙏

dirn added a commit to PyGotham/awards-2020 that referenced this pull request May 6, 2020
How that jazzband/pip-tools#976 has been merged
and released, we can switch to version of the [pip-tools][pip-tools]
[pre-commit][pre-commit] hook provided by [Jazzband][jazzband].

[jazzband]: https://github.com/jazzband
[pip-tools]: https://pypi.org/p/pip-tools
[pre-commit]: https://pre-commit.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvements to functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pre-commit hook for pip-compile
5 participants