简体   繁体   English

为 python 使用多语言版本的黑色预提交钩子

[英]Use pre-commit hook for black with multiple language versions for python

We are using pre-commit to format our Python code using black with the following configuration in .pre-commit-config.yaml :我们使用预提交来格式化我们的 Python 代码,在.pre-commit-config.yaml中使用以下配置使用黑色

repos:
  - repo: https://github.com/ambv/black
    rev: 20.8b1
    hooks:
      - id: black
        language_version: python3.7

As our packages are tested against and used in different Python versions (eg 3.7 , 3.8 , 3.9 ) I want to be able to use the pre-commit Hook on different Python versions.由于我们的包是针对不同的 Python 版本(例如3.73.83.9 )进行测试和使用的,所以我希望能够在不同的 Python 版本上使用预提交挂钩。 But when committing Code eg on Python 3.8 , I get an error due to the language_version in my configuration (see above):但是当在 Python 3.8上提交代码时,由于我的配置中的language_version (见上文),我得到一个错误:

C:\Users\FooBar\Documents\Programmierung\foo (dev -> origin)
λ git commit -m "Black file with correct black version"
[INFO] Initializing environment for https://github.com/ambv/black.
[INFO] Installing environment for https://github.com/ambv/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('c:\\users\\FooBar\\anaconda\\python.exe', '-mvirtualenv', 'C:\\Users\\FooBar\\.cache\\pre-commit\\repobmlg3b_m\\py_env-python3.7', '-p', 'python3.7')
return code: 1
expected return code: 0
stdout:
    RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.7'

stderr: (none)
Check the log at C:\Users\FooBar\.cache\pre-commit\pre-commit.log

How can I enable the pre-commit Hook on different Python-Versions eg only on Python 3?如何在不同的 Python 版本上启用预提交挂钩,例如仅在 Python 3 上启用?

Thanks in advance!提前致谢!

one way would be to set language_version: python3 (this used to be the default for black ) -- the actual language_version you use there doesn't matter all that much as black doesn't use it to pick the formatted language target (that's a separate option)一种方法是设置language_version: python3 (这曾经是black的默认设置)——你在那里使用的实际language_version并不重要,因为 black 不使用它来选择格式化的语言目标(这是一个单独选项)

generally though, you shouldn't need to set language_version as either (1) the hook itself will set a proper one or (2) it will default to your currently running python不过,一般来说,您不需要将language_version设置为 (1) 挂钩本身会设置一个正确的版本,或者 (2) 它默认为您当前正在运行的 python

note also: you're using the twice-deprecated url for black -- it is now psf/black另请注意:您正在使用两次弃用的 url 表示黑色——现在是psf/black

__ __

disclaimer: I created pre-commit and I'm a black contributor免责声明:我创建了预提交并且我是一名黑人贡献者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM