gpt4 book ai didi

python - 获取 Black Python 代码格式化程序以对齐注释

转载 作者:行者123 更新时间:2023-12-05 02:27:58 34 4
gpt4 key购买 nike

是的,我的理解是 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 包裹你的方 block ,这样黑方就不会碰它。

# 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

我通常更喜欢重新定位评论并坚持使用默认的黑色格式。

关于python - 获取 Black Python 代码格式化程序以对齐注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72781400/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com