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

Don't break a line if it's too long because of comments #3329

Closed
josephernest opened this issue Oct 12, 2022 · 6 comments
Closed

Don't break a line if it's too long because of comments #3329

josephernest opened this issue Oct 12, 2022 · 6 comments
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?

Comments

@josephernest
Copy link

josephernest commented Oct 12, 2022

A long line is split into multiple lines if it exceeds the black_line_length setting.
How to not take the comments into consideration to compute the line length?

Example:

# this one should still be broken into multiple lines
long_function(param_1="this is a long line, longer than 80 characters", param_2="this is a long line, longer than 80 characters")  # comment here

# this one SHOULD NOT be broken because the code is smaller than 80 char
short_function(param_1="foo", param_2="bar")  # long comment here, which finally makes the line longer than 200 characters longer than 200 characters longer than 200 characters longer than 200 characters longer than 200 characters longer than 200 characters longer than 200 characters

Is there already a setting for this?

Currently the second example gives:

short_function(
    param_1="foo", param_2="bar"
)  # long comment here, which finally makes the line longer than 200 characters longer than 200 characters longer than 200 characters longer than 200 characters longer than 200 characters longer than 200 characters longer than 200 characters

and I'd prefer to not break the line at all.

@josephernest josephernest added the T: enhancement New feature or request label Oct 12, 2022
@ichard26 ichard26 added T: style What do we want Blackened code to look like? F: linebreak How should we split up lines? F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. and removed T: enhancement New feature or request labels Oct 12, 2022
@josephernest
Copy link
Author

Thanks @ichard26 for the tagging. Do you think there is already a setting for this?

@e-gebes
Copy link

e-gebes commented Oct 12, 2022

I want to mention the special case of pragma comments that exceed the line length limit.
For pragmas of any kind # fmt: off, # pylint: disable=..., and so on, it might make sense to ignore those for computing the line length. Usually, they don't need to be read by humans anyway - it should be enough to see "ah that's the start of a pragma, I don't need to read further".

Moving pragmas to different lines might even break them from working, depending on the implementation of the tool.

@ichard26
Copy link
Collaborator

There is not and I don't think we'll add one either since we try to avoid adding formatting configuration options. We can consider changing the logic in the presence of comments though.

@TomFryers
Copy link
Contributor

Related: #1713.

@felix-hilden
Copy link
Collaborator

This might be way too disruptive and I might not be considering it all the way through, but I'd prefer Black to move the comment on a different line before the line that would be too long. But likely too optimistic 😅

@JelleZijlstra
Copy link
Collaborator

Black will continue to take comments into account while enforcing line length. Line length limits exist to make it easy to read code in environments with fixed-width lines, and code includes comments.

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. F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

6 participants