版本控制集成

使用 pre-commit。一旦您安装了它,将以下内容添加到您的存储库中的 .pre-commit-config.yaml

repos:
  # Using this mirror lets us use mypyc-compiled black, which is about 2x faster
  - repo: https://github.com/psf/black-pre-commit-mirror
    rev: 24.8.0
    hooks:
      - id: black
        # It is recommended to specify the latest version of Python
        # supported by your project here, or alternatively use
        # pre-commit's default_language_version, see
        # https://pre-commit.git.js.cn/#top_level-default_language_version
        language_version: python3.11

随意将 rev 值切换到 Black 的不同版本。

请注意,如果您想在 rev 中使用特定提交,您需要将指定的存储库从镜像切换到 https://github.com/psf/black。我们不鼓励使用分支或其他可变引用,因为挂钩不会像您预期的那样自动更新

Jupyter 笔记本

有一个备用挂钩 black-jupyter,它将 black 的目标扩展到包括 Jupyter 笔记本。要使用此挂钩,只需在 .pre-commit-config.yaml 中将挂钩的 id: black 替换为 id: black-jupyter

repos:
  # Using this mirror lets us use mypyc-compiled black, which is about 2x faster
  - repo: https://github.com/psf/black-pre-commit-mirror
    rev: 24.8.0
    hooks:
      - id: black-jupyter
        # It is recommended to specify the latest version of Python
        # supported by your project here, or alternatively use
        # pre-commit's default_language_version, see
        # https://pre-commit.git.js.cn/#top_level-default_language_version
        language_version: python3.11

注意

black-jupyter 挂钩在 21.8b0 版本中可用。