pypa / hatch

Modern, extensible Python project management
https://hatch.pypa.io/latest/
MIT License
5.81k stars 285 forks source link

Pre-commit hook for `hatch fmt`? #1559

Open blink1073 opened 2 months ago

blink1073 commented 2 months ago

In Jupyter we are using ruff-format in our pre-commit hooks, but hatch fmt has a different set of checks. Would you accept a .pre-commit-hooks.yaml file similar to the one ruff has?

ofek commented 2 months ago

Hey! I'm not familiar because I dislike pre-commit. What would that entail?

blink1073 commented 2 months ago

Hey! Yeah, I forgot about that conversation 😅.

Also, I don't think the file could live here, since you have two "types" of tags for hatchling and hatch.

I think instead we'll add it to our https://github.com/jupyterlab/maintainer-tools repo, and use additional_dependencies: [hatch>=1.12].

The alternative would be to have a separate hatch-pre-commit repo similar to ruff-pre-commit, but I think that is overkill if you don't even like pre-commit. :smile:

Feel free to close this if you like.

ofek commented 2 months ago

I dislike it very much and would certainly never accept use here but I would never want to preclude this use case for others! If you can provide documentation on what exactly I should have in this separate repo then I don't mind doing it eventually.

Just FYI if you use the persistent config method then you don't need Hatch at all and you can use your current setup. This is also better because it works with your IDE.

blink1073 commented 2 months ago

Just FYI if you use the persistent config method then you don't need Hatch at all and you can use your current setup. This is also better because it works with your IDE.

Good to know, thanks!

As I understand it, the separate repo needs a pyproject.toml that depends on a specific version of hatch, and then a .pre-commit-hooks.yaml file that contains something like:

- id: hatch-format
  name: hatch-format
  description: "Run 'hatch fmt --formatter' for extremely fast Python formatting"
  entry: hatch fmt --formatter
  language: python
  types_or: [python, pyi]
  args: []
  require_serial: true
  additional_dependencies: []
  minimum_pre_commit_version: "2.9.2"

Then, whenever you bump the version of hatch, you create corresponding tag in the repo.

The readme would have an example like:

- repo: https://github.com/pypa/hatch-pre-commit
  # Hatch version.
  rev: 1.12.0
  hooks:
    # Run the formatter.
    - id: hatch-format
Simon-Bru commented 1 month ago

I achieved like that :

repos:
  - repo: local
    hooks:
      - id: lint-fmt
        name: Lint (Ruff)
        entry: hatch fmt --check
        language: python
        pass_filenames: false
        require_serial: true