Skip to content

Using uv in pre-commitプリコミットでのuvの使用

An official pre-commit hook is provided at astral-sh/uv-pre-commit.公式のプリコミットフックは astral-sh/uv-pre-commitで提供されています。

To use uv with pre-commit, add one of the following examples to the repos list in the .pre-commit-config.yaml.プリコミットでuvを使用するには、以下のいずれかの例を .pre-commit-config.yamlreposリストに追加します。

To make sure your uv.lock file is up to date even if your pyproject.toml file was changed:あなたのuv.lockファイルが最新であることを確認するために、たとえpyproject.tomlファイルが変更されても:

.pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.9.13
    hooks:
      - id: uv-lock

To keep a requirements.txt file in sync with your uv.lock file:requirements.txtファイルをuv.lockファイルと同期させるには:

.pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.9.13
    hooks:
      - id: uv-export

To compile requirements files:要件ファイルをコンパイルするには:

.pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.9.13
    hooks:
      # Compile requirements
      - id: pip-compile
        args: [requirements.in, -o, requirements.txt]

To compile alternative requirements files, modify args and files:代替の要件ファイルをコンパイルするには、argsfilesを修正します:

.pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.9.13
    hooks:
      # Compile requirements
      - id: pip-compile
        args: [requirements-dev.in, -o, requirements-dev.txt]
        files: ^requirements-dev\.(in|txt)$

To run the hook over multiple files at the same time, add additional entries:複数のファイルに対してフックを同時に実行するには、追加のエントリを追加します:

.pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/uv-pre-commit
    # uv version.
    rev: 0.9.13
    hooks:
      # Compile requirements
      - id: pip-compile
        name: pip-compile requirements.in
        args: [requirements.in, -o, requirements.txt]
      - id: pip-compile
        name: pip-compile requirements-dev.in
        args: [requirements-dev.in, -o, requirements-dev.txt]
        files: ^requirements-dev\.(in|txt)$