python - 如何使用 Black 格式化程序自动中断 Python 代码中的长字符串常量?

标签 python visual-studio-code autoformatting python-black

Python 格式指南,著名的PEP8建议不要使用超过 79 chars 的行.

我可以使用 Black Formatter 轻松地将我的代码自动格式化为最大行长度, 但它不会破坏长字符串。 linter 仍然会提示代码中的 URL 太长,而 Black 也无济于事。

是否可以使用 Black formatter 自动打断长字符串?

最佳答案

是的,这可能是因为 new feature .

首先确保您安装了最新的 Black 格式化程序。现在只需使用选项 --experimental-string-processing 运行黑色。

在 VSCode 中,您可以在 settings.json 文件中配置它:

"python.formatting.blackArgs": [
    "--line-length",
    "99",
    "--experimental-string-processing"
],

顺便说一句,如果你想增加默认行长度,最好在你的 linter 中也更改为相同的值:

"python.linting.flake8Args": [
        "--max-line-length=99",
],

有些团队确实喜欢更长的行,不要让他们以此作为不自动格式化的理由。

顺便说一句,PEP8 支持 greater line length :

Some teams strongly prefer a longer line length. For code maintained exclusively or primarily by a team that can reach agreement on this issue, it is okay to increase the line length limit up to 99 characters, provided that comments and docstrings are still wrapped at 72 characters.

关于python - 如何使用 Black 格式化程序自动中断 Python 代码中的长字符串常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68596593/

相关文章:

python - matplotlib 设置 yaxis 标签大小

gruntjs - 在VSCode中使用目标/参数运行grunt任务

python - 对 Vim 有用的 Python 命令列表?

visual-studio-code - 防止保存时的VSCode自动格式删除最终换行符

python - statsmodels:一起打印多个回归模型的摘要

python - 将 PyMySQL 与 MariaDB 结合使用

python - Python 中模运算符的逆运算

visual-studio-code - 在 VS 代码资源管理器中导航

angular - 在 Visual Studio Code 中使用 Jest 调试 Angular 测试

css - Aptana3 CSS 缩进