[英]In python, how to tweak Black formatter, if possible?
I know that Black is an opinionated formatter, but I love everything it does except one major thing.我知道 Black 是一个固执己见的格式化程序,但我喜欢它所做的一切,除了一件大事。 When I have a function with multiple arguments, instead of displaying it like this:当我有一个带有多个参数的函数时,而不是像这样显示它:
def example_function(arg_1: str, arg_2: bool, arg_3: int = 0, arg_4: int = 1, arg_5: float = 0.0):
pass
I'd rather display it as follows for readability为了便于阅读,我宁愿将其显示如下
def example_function(
arg_1: str,
arg_2: bool,
arg_3: int = 0,
arg_4: int = 1,
arg_5: float = 0.0
):
Is this achievable with Black or some other formatter?这可以通过 Black 或其他格式化程序实现吗? I have this problem several times and it makes me consider not to use Black, either something else or nothing at all.我多次遇到这个问题,这让我考虑不使用黑色,无论是其他东西还是什么都不使用。 Any ideas or comments?任何想法或意见?
This is due to the default line length for black
being longer than you'd like – 88 characters per line .这是因为black
的默认行长比您希望的要长——每行 88 个字符。
To decrease the line length, you can use the --line-length
flag as documented here:要减少行长度,您可以使用--line-length
标志,如此处所述:
https://github.com/psf/black/blob/master/docs/installation_and_usage.md#command-line-options https://github.com/psf/black/blob/master/docs/installation_and_usage.md#command-line-options
For example:例如:
$ black --line-length 80 example.py
Black explains the --line-length
setting in more detail here: Black 在这里更详细地解释了--line-length
设置:
https://github.com/psf/black/blob/master/docs/the_black_code_style.md#line-length https://github.com/psf/black/blob/master/docs/the_black_code_style.md#line-length
Line length线长
You probably noticed the peculiar default line length.您可能注意到了特殊的默认行长。 Black defaults to 88 characters per line, which happens to be 10% over 80. This number was found to produce significantly shorter files than sticking with 80 (the most popular), or even 79 (used by the standard library).黑色默认为每行 88 个字符,恰好超过 80 个字符的 10%。发现这个数字产生的文件比坚持使用 80(最受欢迎)甚至 79(由标准库使用)要短得多。 In general, 90-ish seems like the wise choice .总的来说, 90-ish 似乎是明智的选择。
If you're paid by the line of code you write, you can pass
--line-length
with a lower number.如果您按您编写的代码行付费,您可以通过--line-length
使用较低的数字。 Black will try to respect that.黑色会尽量尊重这一点。 However, sometimes it won't be able to without breaking other rules.但是,有时它无法不违反其他规则。 In those rare cases, auto-formatted code will exceed your allotted limit.在极少数情况下,自动格式化的代码将超出您分配的限制。You can also increase it, but remember that people with sight disabilities find it harder to work with line lengths exceeding 100 characters.您也可以增加它,但请记住,有视力障碍的人发现处理超过 100 个字符的行变得更加困难。 It also adversely affects side-by-side diff review on typical screen resolutions.它还会对典型屏幕分辨率的并排差异审查产生不利影响。 Long lines also make it harder to present code neatly in documentation or talk slides.长行也使得在文档或幻灯片中整齐地呈现代码变得更加困难。
Emphasis on the final paragraph.强调最后一段。
I'd recommend just keeping the default settings.我建议只保留默认设置。 The beauty of Black is that it chooses for you, and therefor preempts any arguments about which way is "best" .黑色的美妙之处在于它为你选择,因此抢占了关于哪种方式是“最好的”的任何争论。
This is now possible by adding a trailing comma to your last argument.现在可以通过在最后一个参数中添加一个尾随逗号来实现。
In your example you would write instead:在您的示例中,您将改为编写:
def example_function(
arg_1: str,
arg_2: bool,
arg_3: int = 0,
arg_4: int = 1,
arg_5: float = 0.0, # <-- Notice the trailing comma
):
When using the Black playground at https://black.now.sh , I found that your function was reformatted exactly like that when the line length was short enough (in particular, at 78 characters).在https://black.now.sh使用 Black playground 时,我发现您的函数的格式与行长度足够短(特别是 78 个字符) 时完全一样。
It might be nice if there were a special configuration option that controlled the line length specifically for function parameter lines.如果有一个特殊的配置选项来控制专门用于函数参数行的行长度,那可能会很好。 BUT to seems to me that Black's configuration-free approach means that there is no way to control this with any more tailored option.但是在我看来,Black 的免配置方法意味着无法通过任何更定制的选项来控制它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.