[英]Getting Black Python code formatter to align comments
是的,我理解black
在让它表现得不同方面几乎没有回旋余地,但我想知道处理此类事情的最佳方法(我的原始代码):
@dataclass
class Thing1:
property1: int # The first property.
property2: typing.List[int] # This is the second property
# and the comment crosses multiple lines.
现在,当我通过black
运行它时,它给了我:
@dataclass
class Thing1:
property1: int # The first property.
property2: typing.List[int] # This is the second property
# and the comment crosses multiple lines.
这并不是我想要的那样。
有没有办法让评论保持一致? 我不在乎它在每个字段中从哪一列开始,但是返回可读性会很好,每个字段中的注释都排成一行:
@dataclass
class Thing1:
property1: int # The first property.
property2: typing.List[int] # This is the second property
# and the comment crosses multiple lines.
如果没有办法做到这一点,我可能会在每个字段之前放置多行注释,或者确保所有注释都放在一行中,并在数据类定义的末尾添加更长的解释性注释:
@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.
至少对于数据字段来说,这仍然是局部的。 但如果可能的话,我宁愿找到一种方法让它与我最初的评论相似。
乐于接受任何建议。
你可以用# 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
我通常更喜欢重新定位评论并坚持使用默认的黑色格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.