Skip to content

Preserve alignment of inline comments #4651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pekkaklarck opened this issue Apr 14, 2025 · 0 comments
Open

Preserve alignment of inline comments #4651

pekkaklarck opened this issue Apr 14, 2025 · 0 comments
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. T: style What do we want Blackened code to look like?

Comments

@pekkaklarck
Copy link

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.

@pekkaklarck pekkaklarck added the T: style What do we want Blackened code to look like? label Apr 14, 2025
@JelleZijlstra JelleZijlstra added the F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. label Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

2 participants