Skip to content

Preserve alignment of inline comments #4651

@pekkaklarck

Description

@pekkaklarck

Describe the style change

If inline comments have been aligned, that has most likely been done for a reason and breaking the alignment is likely to make the code worse. As an example, we have this code in our project:

TypeHint = Union[
    type,                     # Actual type.
    str,                      # Type name or alias.
    UnionType,                # Union syntax (e.g. `int | float`).
    'tuple[TypeHint, ...]'    # Tuple of type hints. Behaves like a union.
]

Examples in the current Black style

Currently the above example is formatted like this:

TypeHint = Union[
    type,  # Actual type.
    str,  # Type name or alias.
    UnionType,  # Union syntax (e.g. `int | float`).
    'tuple[TypeHint, ...]',  # Tuple of type hints. Behaves like a union.
]

Desired style

I'd prefer the original formatting of comments to be preserved. Well, I would be fine if there would be just two spaces between the longest item and its comment instead of the current four.

Additional context

The situation changes if the code itself needs to be formatted or if inline comments are so long that they violate the max line length. Neither of these are true in our case and, in my opinion, the formatted code is clearly worse than the original.

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: commentsThe syntactic kind. Not in the language grammar, always on our minds. Best bugs.T: styleWhat do we want Blackened code to look like?

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions