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

Running tools through pre-commit #14

Open
eavanvalkenburg opened this issue Aug 22, 2024 · 3 comments
Open

Running tools through pre-commit #14

eavanvalkenburg opened this issue Aug 22, 2024 · 3 comments

Comments

@eavanvalkenburg
Copy link

I would like the ability to use uv tool run ruff with this! Especially ruff and mypy!

@UnknownPlatypus
Copy link
Contributor

I don't understand the point of running uv tool via pre-commit. This seems like adding layers upon layers of indirection.

pre-commit is already a tool designed to install hook in isolated environment, similarly to what uv tool does.

Adding the feature you ask for would mean that pre-commit will create a venv inside which uv is installed to create a venv to install the tool you ask for. Maybe you are looking for https://github.com/tox-dev/pre-commit-uv, this project uses uv to handle the python venv creation inside pre-commit.

For ruff, I would recommended the supported pre-commit hook:

  - repo: https://github.com/charliermarsh/ruff-pre-commit
    rev: v0.6.1
    hooks:
      - id: ruff

For mypy there is https://github.com/pre-commit/mirrors-mypy (but mypy is slow, pre-commit is maybe not the right place to run it)

@CarliJoy
Copy link

CarliJoy commented Dec 23, 2024

I also would be in favour of running mypy through this pre-commit hook. But not with uv tool run but uv run.

Why not https://github.com/pre-commit/mirrors-mypy ?
Because it doesn't create the environment. I would have to add all dependencies manually to the pre-commit hook.
Using uv run mypy everything is properly installed using the pyproject.toml configuration.

Indeed it would be nice if pre-commit would support creating a hook that contains the current project installed in develop mode but it doesn't. So it might be an idea to ask for PEP 735 support in pre-commit in order to use a dependency-groups as additional_dependencies.

But this way the env pre-commit caches would not update if the lock file updates.
Using uv run mypy ensures all used environments are using the same version.
Also running it locally i uses the venv already created.

I am currently helped myself with:

  - repo: local
    hooks:
      - id: mypy
        name: mypy
        entry: uv run mypy
        args: []
        language: system
        types: [python]

Which requires uv to be installed in $PATH.

Having it here this requirement would vanish.

@danielhollas
Copy link

I've opened WIP branch on my fork for the uv-run hook, PR at #42.

@CarliJoy would you mind giving it a go? I am interested if it works for your mypy use case. Something like this in the .pre-commit-config.yaml should work

repos:   
  - repo: https://github.com/danielhollas/uv-pre-commit
    rev: c2c5e6a1c7d17dd3c237aae6557d15a142b89b8c
    hooks:
   - id: uv-run    
      name: mypy type check    
      args: [mypy]    
      pass_filenames: true    
      types: [python]          

There are two disadvantages to this approach:

  1. It won't work on pre-commit.ci service since it needs network access, which is forbidden during hook execution on pre-commit.ci.
  2. If you want to execute a single hook, you will need to refer to if via the uv-run hook id which might be non-obvious e.g.
$ pre-commit run uv-run --all-files

This would get awkward once you'd want to run multiple things via uv run, you can still define multiple hooks with the same id, but you wouldn't be able to refer to them via a unique name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants