如何修复使用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字段验证失败,可通过以下两种方式修复:
- 移除
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
- 指定正确的
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
相关产品推荐
相关产品推荐

