You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复使用pre-commit搭配black时出现的InvalidManifestError?

解决pre-commit搭配black 23.11.0时的InvalidManifestError错误

问题场景

使用pre-commit运行black 23.11.0版本时,触发了InvalidManifestError异常。

配置文件片段(.pre-commit-config.yaml)

repos:
-   repo: https://github.com/psf/black
    rev: 23.11.0
    hooks:
    -   id: black
        types: []
        files: ^.*.pyi?$  # format .py and .pyi files

错误信息

An error has occurred: InvalidManifestError: 
==> File /Users/robot/.cache/pre-commit/repoxhmwyits/.pre-commit-hooks.yaml
==> At Hook(id='black')
==> At key: stages
==> At index 0
=====> Expected one of commit, commit-msg, manual, merge-commit, post-checkout, ...
Check the log at /Users/robot/.cache/pre-commit/pre-commit.log

解决方案

错误根源是配置中types: []的设置导致钩子的stages字段验证失败,可通过以下两种方式修复:

  1. 移除types: []配置项
    black钩子默认会处理Python相关文件,结合你设置的files规则已能覆盖.py和.pyi文件,无需手动清空types:
repos:
-   repo: https://github.com/psf/black
    rev: 23.11.0
    hooks:
    -   id: black
        files: ^.*.pyi?$  # format .py and .pyi files
  1. 指定正确的types值
    若需明确指定文件类型,将types设为[python](对应.py和.pyi文件):
repos:
-   repo: https://github.com/psf/black
    rev: 23.11.0
    hooks:
    -   id: black
        types: [python]
        files: ^.*.pyi?$  # format .py and .pyi files

修改配置后重新运行pre-commit,即可正常使用black钩子。

内容的提问来源于stack exchange,提问作者zerocewl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 04:00:23