是的,我理解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.

这并不是我想要的.

有没有办法让黑人保持 comments 的一致性?我不在乎它从每个字段的哪个列开始,但最好返回可读性,将each个字段内的注释排列起来:

@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.

对于数据字段来说,这至少仍然是somewhat个局部变量.但如果可能的话,我宁愿找到一种方法,让它把 comments 排成与我最初的 comments 类似的行.

很乐意接受任何建议.

推荐答案

你可以用# 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

我通常更喜欢重新定位注释,并坚持默认的黑色格式.

Python相关问答推荐

为什么这个带有List输入的简单numba函数这么慢

Python键入协议默认值

在极性中创建条件累积和

迭代嵌套字典的值

Scrapy和Great Expectations(great_expectations)—不合作

如何并行化/加速并行numba代码?

在两极中过滤

判断solve_ivp中的事件

寻找Regex模式返回与我当前函数类似的结果

从旋转的DF查询非NaN值

根据客户端是否正在传输响应来更改基于Flask的API的行为

为什么t sns.barplot图例不显示所有值?'

当HTTP 201响应包含 Big Data 的POST请求时,应该是什么?  

极点替换值大于组内另一个极点数据帧的最大值

为什么我只用exec()函数运行了一次文件,而Python却运行了两次?

如果服务器设置为不侦听创建,则QWebSocket客户端不连接到QWebSocketServer;如果服务器稍后开始侦听,则不连接

FileNotFoundError:[WinError 2]系统找不到指定的文件:在os.listdir中查找扩展名

如何在Django查询集中生成带有值列表的带注释的字段?

PyTorch变压器编码器中的填充掩码问题

如何判断变量可调用函数的参数是否都属于某个子类?