hatch icon indicating copy to clipboard operation
hatch copied to clipboard

Pre-commit hook for `hatch fmt`?

Open blink1073 opened this issue 1 year ago • 5 comments
trafficstars

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?

blink1073 avatar Jun 09 '24 16:06 blink1073

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

ofek avatar Jun 09 '24 17:06 ofek

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.

blink1073 avatar Jun 10 '24 12:06 blink1073

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.

ofek avatar Jun 10 '24 15:06 ofek

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

blink1073 avatar Jun 11 '24 01:06 blink1073

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

Simon-Bru avatar Jul 11 '24 13:07 Simon-Bru