如何使用 Black 格式化程序自动中断 Python 代码中的长字符串常量?
How to automatically break long string constants in Python code using Black formatter?
Python格式指南,著名的PEP8 recommends no line longer than 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.
对于较新版本的 black(例如 22.1.0),此功能现在是 --preview
标志的一部分。
Python格式指南,著名的PEP8 recommends no line longer than 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.
对于较新版本的 black(例如 22.1.0),此功能现在是 --preview
标志的一部分。