当使用 Python Black包运行 lint 检查时,会出现错误:
ImportError:无法从“click”导入名称“_unicodefun”(/Users/robot/.cache/pre-commit/repo3u71ccm2/py_env-python3.9/lib/python3.9/site-packages/click/ init .py)`
在研究过程中,我发现了以下相关问题:
我怎么解决这个问题?这是 linter 的误报吗?我需要修改我的代码吗?
我想通过blackpython 格式化程序忽略特定的多行代码。特别是,这用于np.array在格式化时变得丑陋的或矩阵构造。下面是示例。
np.array(
[
[1, 0, 0, 0],
[0, -1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, -1],
]
)
# Will be formatted to
np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]])
Run Code Online (Sandbox Code Playgroud)
我在github 中发现了这个问题black,但这仅适用于内联命令,这不是我在这里所拥有的。
对于多行代码,我可以做些什么来实现这一点?
我知道对于 jupyter notebooks 和 jupyter lab,有可用的代码格式化程序扩展,例如nb_black或blackcellmagic. 但是,当我安装它们时,它似乎不适用于 google colab。
您知道 colab 中是否有任何本机选项或格式化代码的扩展程序(符合 pep8)?
我正在针对 Python 脚本运行 Black 代码格式化程序,但它不会重新格式化文档字符串的行长度。例如,给出以下代码:
def my_func():
"""
This is a really long docstring. This is a really long docstring. This is a really long docstring. This is a really long docstring. This is a really long docstring. This is a really long docstring.
"""
return
Run Code Online (Sandbox Code Playgroud)
当针对此脚本运行 Black 时,行长度不会改变。如何确保文档字符串在运行 Black 时得到格式化?
我正在尝试使用预提交来管理Black作为 Git 预提交挂钩,但我一定是做错了。
在我的预提交配置文件中,我有:
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
Run Code Online (Sandbox Code Playgroud)
我期望发生的是 Black 只修改暂存文件,并且提交成功。因为 Black 的全部意义在于它自动强制执行 Python 代码样式规则,所以不会提出任何问题。
当我暂存(非黑色兼容)文件并尝试提交时实际发生的情况:黑色继续修改文件以使其符合预期……但问题是它返回了“失败”。所以提交失败。然后我必须取消暂存文件,然后在再次提交之前重新暂存它......只有这样提交才能成功。
这是一个巨大的烦恼,不可能是预期的工作流程?
我究竟做错了什么?
我觉得布莱克正在做一些不符合(与我的组织)的事情,所以我试图忽略某些规则。
下面的示例和相关链接
PEP 8: whitespace before ':'
我的组织(编码标准)并不优先考虑黑人认为正确的事情,而是想要一种定制黑人配置的方法。
我没有在 Black 文档中看到任何提及忽略规则的内容https://github.com/psf/black#command-line-options。
他们给出了忽略 Flake8 规则的示例,但似乎没有任何关于他们自己产品的文档。
我尝试在 Ubuntu 20.04 上的 VS Code 上使用black 作为 Python 的格式化程序,但它无法自动保存。
我已经选择black在Python>Formatting:Provider. 我使用 prettier 作为默认格式化程序,为其添加了 .prettierignore、禁用并卸载,以确保它不会干扰黑色。我还添加了自定义路径./local/bin/black。当我通过终端运行它时它可以工作。我该如何让它发挥作用?
{
editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"-l 120"
],
"editor.formatOnType": true,
"python.formatting.blackPath": "./local/bin/black"
}
Run Code Online (Sandbox Code Playgroud) 我想在预提交中配置黑色并从检查任何迁移文件夹中排除预提交。
我的 pyproject.toml 看起来像这样
[tool.black]
line-length = 79
target-version = ['py37']
include = '\.pyi?$'
exclude = '''
(
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
| ^migrations/
'''
Run Code Online (Sandbox Code Playgroud)
我还配置了预提交。但是在运行pre-commit run --all-files
黑色格式迁移文件夹时,我如何配置黑色
每当有一个内联断言规则需要针对 bool 语句进行验证时,在 VSCode 中使用 python black 格式化程序都会破坏该行,导致 flake8 发出有关规则 W503 的警告
line break before binary operatorflake8(W503)
assert (
...
!= ...
)
Run Code Online (Sandbox Code Playgroud)
有没有解决这个问题而不是忽略该规则的方法?
我需要在 Vs Code 中缩进我的 python 文件。我按照正常程序,
Run Code Online (Sandbox Code Playgroud)On Windows Shift + Alt + F On Mac Shift + Option + F On Linux Ctrl + Shift + I
但我的问题是每次我尝试格式化 python 文件时,它都会说
黑色不支持“格式选择”
所以有人可以解释这里出了什么问题?我的python版本是Python 3.7.6
VS代码详情:
Version: 1.46.0 (user setup)
Commit: a5d1cc28bb5da32ec67e86cc50f84c67cc690321
Date: 2020-06-10T09:03:20.462Z
Electron: 7.3.1
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Windows_NT x64 10.0.18363
Run Code Online (Sandbox Code Playgroud) python formatting visual-studio-code vscode-settings python-black
python-black ×10
python ×6
pre-commit ×2
docstring ×1
flake8 ×1
formatting ×1
git ×1
githooks ×1
pep8 ×1