black: Line too long: Comments
Problem
Consider the below comment:
# this comment is longer than what is specified in line length arguments passed to black blah blah blah blah blah blah
If I run black on this file the comment remains intact, even though its clearly over the line length limit.
Split the comment to fit into the line length
Something like
# this comment is longer than what is
# specified in the line length
...
...
The same problem also exists for strings as well but I believe that’s better left intact but for comments I think splitting them up into multiple lines makes sense.
If this is a feature that you guys believe will be a good addition to black, then I can work on this and open a PR.
Thanks
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 39
- Comments: 31
Commits related to this issue
- Run pre-commit autoupdate Dont use rev: stable suggestion from https://github.com/psf/black/blob/main/docs/integrations/source_version_control.md https://github.com/psf/black/issues/420 Also black ... — committed to felker/deephyper_pytorch_layers by felker 3 years ago
- lint: ignore flake8 line too long for ignored types Adding a `# type: ignore` comment to a couple of places triggered flake8's line too long check. Running `make reformat` did nothing for these - bl... — committed to miketheman/warehouse by miketheman 2 years ago
- lint: ignore flake8 line too long for ignored types Adding a `# type: ignore` comment to a couple of places triggered flake8's line too long check. Running `make reformat` did nothing for these - bl... — committed to miketheman/warehouse by miketheman 2 years ago
- chore: add mypy (#10874) * chore(deps): install mypy in lint.in Signed-off-by: Mike Fiedler <miketheman@gmail.com> * chore(deps): include more types-* packages for mypy These were suggested ... — committed to pypi/warehouse by miketheman 2 years ago
- chore: add mypy (#10874) * chore(deps): install mypy in lint.in Signed-off-by: Mike Fiedler <miketheman@gmail.com> * chore(deps): include more types-* packages for mypy These were suggested ... — committed to domdfcoding/warehouse by miketheman 2 years ago
I’m going to go ahead and say that Black will not split comments.
# type,# noqa, etc.), and splitting them up will break them. We can hardcode support for some pattern, but it’s going to be a never-ending battle.This seems risky. Comments are often commented out code or other structured data. I think we should not merge short comments, but splitting long ones is probably OK.
What’s the status of this issue?
In my project, we run
blackto reformat our files. Then we runpylint, which complains about the long comment lines, and we have to manually (!!) shorten them one-by-one. It always feels strange that I have to do manually something that can be so easily automated, especially considering that we do use a tool (black) whose job is exactly that, to automatically reformat my files.I understand that there may be complications that I don’t see, and it’s fine to have it as a non-default option. I’ll be happy to see it happen.
Maybe we just need to write a separate tool for this, outside of Black? While I personally would like to have this feature in Black, because it would be convenient for me, I acknowledge that it might not fit with Black’s goals.
i agree, black behaves very oddly with comments
becomes
which neither improves readability, nor keeps the comment on the same line as either the while loop or the condition, which it may have referred to
with regards to comments, i would recommend:
IE do this
Or this:
Most likely a new configuration option is a no-go. But seems like there is some acceptance for just splitting them, despite the edge cases. As I said, for starters we could do it for dedicated comment lines only, not inline comments. One discussion that should be had is the style of split. I quite like equalising the line lengths if one would be radically shorter. But that probably is also related to string processing, which I’m not familiar with (yet).
So I’d prefer the last option.
I would also really love to have this made possible, maybe as a default off option (although Black doesn’t like having too many options, which I respect). For what it’s worth, ClangFormat does reflow comments so they fit within the specified line length, and having used it for a few years now, it rarely causes any annoyance.
My opinion:
Yes.
It stays as it is, and you can manually rewrite it if you want it back on one line.
ClangFormat does it this way, and in my experience it works well.
For comment lines specifically, there is a good chance we’ll never split them. There are other issues in https://github.com/psf/black/labels/F%3A linetoolong that we should fix though.
I’d like to state, that if we take care of some of the simple scenarios here, that it becomes next years default and it’s only gated by the
--previewflag. I’m still a big proponent of as little formatting configuration options as possible please.That’s a valid concern, and different from other languages (which would just parse the still valid code and reformat it properly if it gets wonky when un-commenting some code).
I still think this is fine, though. When you uncomment some commented out code, it’s not unreasonable to expect that you also fix it if its formatting gets weird.
Just to make this more difficult: I prefer the first. That will generally make diffs smaller, if say I decide to append some more text.
I would go for a config option, something like
split-singleline-comments, because as already mentioned in the thread there are complications when it comes to splitting in-line comments and I would like if a default option covered both the cases to avoid confusion.Also if no-one is currently working on this, can you assign this issue to me, so I can work and open a PR.