Skip to content
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

Inline comments can make ugly formatting for actual code #591

Closed
hyperknot opened this issue Nov 1, 2018 · 4 comments
Closed

Inline comments can make ugly formatting for actual code #591

hyperknot opened this issue Nov 1, 2018 · 4 comments
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. R: rejected This will not be worked on

Comments

@hyperknot
Copy link

Black 32eed7

Playground link

Options

--line-length=60
--py36

Input

if now.second < 30:  # Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    continue

Output

if (
    now.second < 30
):  # Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    continue

Expected

The code should not be changed, especially not to this ugly form, based on how long a comment is afterwards.

@benmoose
Copy link

I agree that the generated code looks bad.

What are thoughts on moving inline comment that exceed the line-length to the proceeding line?

# Lorem ipsum dolor sit amet, consectetur adipiscing elit.
if now.second < 30:
    continue

@zsol zsol added the F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. label Feb 16, 2019
@ambv ambv added the R: rejected This will not be worked on label Mar 4, 2020
@ambv
Copy link
Collaborator

ambv commented Mar 4, 2020

Black made those gymnastics with the parentheses and the comment because the comment was too long. Black cannot move that up on its own as it changes the position of the comment relative to content and it might change meaning of what we're looking at. More importantly, in the general case it might not be as simple as moving "one line up".

The solution is for you to manually move the comment up and reformat.

@ambv ambv closed this as completed Mar 4, 2020
@thatch
Copy link

thatch commented Mar 4, 2020

This is contrary to how ignores in flake8 work, and I would rather it leave the line alone than change a single line to multiline with the comment at the end if the goal is to be safe.

@ambv
Copy link
Collaborator

ambv commented Mar 4, 2020

The goal for this tool is to reformat code so that it fits the line length limit while still executing the same. It's concerned with safety in this regard alone. "To be safe" is vague and subjective otherwise.

What Black does in this case is simple: the inline comment was originally put after the colon token and it stays after the colon token. I don't think it's a terrible choice but agreed, this does interfere with pragma-style comments and we'll be addressing some of that in #379.

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. R: rejected This will not be worked on
Projects
None yet
Development

No branches or pull requests

5 participants