-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Option to ignore in-line comments #3450
Comments
I just wanted to voice my concern with this as well. Even setting the non-respecting of Can we please please please have an option for Black to ignore comments completely? EDIT: I am running with the |
I assume that the developers are quite busy, but I would be very happy to receive some feedback from them on this topic after 7 months 🙂 At the moment, I try to use the second workaround in #3450 (comment) to deal with this problem. However, this is quite annoying, because, as pointed out above, the simplicity of in-line comments is lost. |
There is a clear bug here in that Otherwise, I'm not sure what it means to "ignore" in-line comments as this issue asks for. Does it mean to ignore them when computing the line length, potentially leaving lines longer than the requested line length? I don't think that would be an improvement. |
Yes, ignore them when computing line length essentially. In other words, pretend they are not even there. I'd have thought that's what Black does by default, especially given this seciton in the Black code style guide: https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#comments. |
Yes, this is what I originally wanted. The reason is that our codebase has some lengthy in-line comments. Applying black to such code results in a formatting which we find confusing... For instance,
becomes
which we find quite hard to read. Thus, making black ignore comments would be one solution to have a "nicer" formatting in these cases. However, after reconsideration, I think that ignoring in-line comments may not the best option because PEP8 also considers in-line comments for the line length (our code base didn't so far). Instead of ignoring in-line comments, maybe it would be better if black could put in-line comments above the code if they exceed the line length so that the above code example is formatted as
This would honor the line length and also improve the formatting IMO. The only downside of this approach is that it is not clear how to deal with in-line comments that appear in a method chain, e.g.,
In this case, it is not sensible to put all in-line comments above the method chain. So in this case, black's formatting should not be changed (?). |
This section mentions that So maybe, if no new feature to black is added, one could extend the documentation in this regard so that people better know what to expect? |
"...may sometimes be moved because of formatting changes" sounds more like that some other formatting change that would've happened anyways without the comment being there might instead affect the comment. What we are observing here is that the comment itself is triggering the change. To me the docs don't seem to imply that at all and are extremely unclear in that regard... |
Thanks for the clarifications. In that case, I'm going to reject this issue. Black in general aims to make all code fit within the line length limit. This is so that when code is read in an environment with fixed-size lines, the whole line is visible. Comments are part of the code and it's important that readers of the code can also read the comments, so they should also fit within the line length. Indeed, one of our most commonly liked rejected issues asks us to split up long comments into multiple lines (#1713). That suggests that many users would be upset if we allowed comments to break the line length in more cases. I don't think the linked documentation is unclear, as it should be the default that any aspect of the code, including comments, is counted for line length purposes. However, if you would like to improve the docs, please file a PR and I'll take a look. Similarly, if you have concrete proposals for improving how to handle comments (such as moving them to the line above), please open a new issue with explicit examples of how you would like code to be formatted. Make sure you don't overlap one of the existing issues in https://github.com/psf/black/issues?q=comment+is%3Aissue+label%3A%22F%3A+comments%22+, though. |
Consider this code snippet
After running black in https://black.vercel.app/ this becomes
which I find really confusing and by far inferior to the original formatting. Note that black has formatted the code although the line ended with
#fmt: skip
(!) (probably related to #3009). Indeed, black does not format the code when I remove#fmt: skip
because the line length is then small enough.So I wonder whether it makes sense to make black ignore in-line comments?
At the moment the only possible solution to avoid this ugly formatting is
Use
which I don't like because I then have to add two empty lines for an in-line comment.
Put the comment on an empty line
which I don't like because this also adds and additional line of code and is not useful if one uses chall chains and wants to add a comment to a particular line of the chain.
The text was updated successfully, but these errors were encountered: