[英]Getting Black Python code formatter to align comments
Yes, I'm, of the understanding that black
gives very little leeway in getting it to act differently but I was wondering about the best way to handle something like this (my original code):是的,我理解
black
在让它表现得不同方面几乎没有回旋余地,但我想知道处理此类事情的最佳方法(我的原始代码):
@dataclass
class Thing1:
property1: int # The first property.
property2: typing.List[int] # This is the second property
# and the comment crosses multiple lines.
Now, when I run that through black
, it gives me:现在,当我通过
black
运行它时,它给了我:
@dataclass
class Thing1:
property1: int # The first property.
property2: typing.List[int] # This is the second property
# and the comment crosses multiple lines.
which is not really laid out as I'd like.这并不是我想要的那样。
Is there a way to get black to keep the comments lined up?有没有办法让评论保持一致? I don't care which column it starts in per field, but it would be nice to return to the readable nature, with the comments within each field being lined up:
我不在乎它在每个字段中从哪一列开始,但是返回可读性会很好,每个字段中的注释都排成一行:
@dataclass
class Thing1:
property1: int # The first property.
property2: typing.List[int] # This is the second property
# and the comment crosses multiple lines.
If there's no way to do this, I'll probably either place multi-line comments before each field, or ensure all comments fit on the one line and add lengthier explanatory notes at the end of the dataclass definition:如果没有办法做到这一点,我可能会在每个字段之前放置多行注释,或者确保所有注释都放在一行中,并在数据类定义的末尾添加更长的解释性注释:
@dataclass
class Thing1:
property1: int # The first property.
# This is the second property and the comment, while it can
# have more characters, still crosses multiple lines.
property2: typing.List[int]
@dataclass
class Thing2:
property1: int # The first property.
property2: typing.List[int] # This is the second property.
# Note that the comments above now fit on the same line as the
# field, and this comment here is meant to provide any needed
# multi-line detail for ALL fields in this class.
That at least is still somewhat local to the data fields.至少对于数据字段来说,这仍然是局部的。 But I'd rather find a way to have it line up comments similar to what I originally had, if possible.
但如果可能的话,我宁愿找到一种方法让它与我最初的评论相似。
Happy to entertain any suggestions.乐于接受任何建议。
You can wrap your block with # fmt: on/off
, so Black doesn't touch it.你可以用
# fmt: on/off
包裹你的方块,这样黑方就不会碰它。
# fmt: off
@dataclass
class Thing1:
property1: int # The first property.
property2: typing.List[int] # This is the second property
# and the comment crosses multiple lines.
# fmt: on
I usually prefer to relocate the comments and stick with default Black formatting.我通常更喜欢重新定位评论并坚持使用默认的黑色格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.