简体   繁体   English

让 Black Python 代码格式化程序对齐注释

[英]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.

相关问题 黑色格式化程序 - Python - The Black Formatter - Python VS Code Python + 黑色格式化程序参数 - python.formatting.blackArgs - VS Code Python + Black formatter arguments - python.formatting.blackArgs 为什么不需要加载 Python 格式化程序(黑色)和 linter(pylint)和 vs 代码? - Why no need to load Python formatter (black) and linter (pylint) and vs code? Python 黑色代码格式化程序不格式化文档字符串行长度 - Python Black code formatter doesn't format docstring line length 如何在Python代码中包装和对齐注释? - How to wrap and align comments in Python code? 如何在 Spyder 中使用代码格式化程序 Black? - How to use code formatter Black with Spyder? 即使在将 Black 配置为格式化程序后,获取“扩展 'Python Language Basics' 无法格式化 ~'/'” - Getting “Extension 'Python Language Basics' cannot format ~'/'” even after configuring Black as formatter 在 python 中,如果可能,如何调整黑色格式化程序? - In python, how to tweak Black formatter, if possible? 如何使用黑色格式化程序自动中断 Python 代码中的长字符串常量? - How to automatically break long string constants in Python code using Black formatter? 使用黑码格式化程序的问题; 无法导入_ast3 - Problem with using Black code formatter; can't import _ast3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM