Line too long: Long from import
Describe the bug
I originally posted on SO here.
I have a python project with Flake8 and Black installed. I'm using VSCode, with the following workspace configuration in settings.json: "black-formatter.args": ["--line-length=80"],. I also tried: "black-formatter.args": ["--line-length, "80"],
This arg is also manually addedin the settings UI for the user, WSL, and workspace.
I also tried running black in the terminal with the arg like this: black --line-length=80 conftest.py
The output:
All done! ✨ 🍰 ✨
1 file left unchanged.
When I manually break the long line, black actually undoes the change. I can't just configure the project to ignore this error, because this is a shared project and this style has to be enforced.
I'm using WSL2 inside Windows 11, python version 3.11.5. I'm also using poetry version 1.7.1. Any help is appreciated.
To Reproduce
Unfortunately I can't post exact lines of code since this is for work, but any lines above 100 characters cause this issue, but something like:
from src.pkg.core.application.another_pkg.yet_another_pkg.this_is_getting_long.some_file_with_a_long_name import (
Something,
)
Is not formatted, and when breaking it manually like:
from src.pkg.core.application.another_pkg.yet_another_pkg. \
this_is_getting_long.some_file_with_a_long_name import (
Something,
)
Black reverts the line break.
I've also seen this happen with comments, though line breaks arent reverted in this case. Flake8 does complain about long comments at least for me:
"""
Some really really really really really really really really really really really really really really really really long comment
"""
Black should break long lines (according to the config I used, any line > 80 characters), or at least not revert changes made to manually break lines.
Environment
Windows 11, WSL2, python 3.11.5, poetry 1.7.1
- Black's version: black, 23.12.1 (compiled: yes)
- OS and Python version: Python (CPython) 3.11.5, Windows 11, WSL2 running Ubuntu 22.04.3 LTS
Could you give a sample of the code that Black is not formatting correctly?
Something like:
from src.pkg.core.application.another_pkg.yet_another_pkg.this_is_getting_long.some_file_with_a_long_name import (
Something,
)
Is not formatted, and when breaking it manually like:
from src.pkg.core.application.another_pkg.yet_another_pkg. \
this_is_getting_long.some_file_with_a_long_name import (
Something,
)
Black reverts the line break.
I've also seen this happen with comments, though line breaks arent reverted in this case. Flake8 does complain about long comments at least for me:
``` python
"""
Some really really really really really really really really really really really really really really really really long comment
"""
I'll add this to the original question. (I edited the examples to reflect my current config of line length being 80)
Thanks. The import issue is new, but I'm not sure we'd want to fix it, since the proposed formatting is ugly and there's a good chance it will conflict with import formatting tools.
The long triple-quoted string (not a comment) is essentially #1617. I don't think we'll fix that either, as the line breaks in a triple-quoted string may be significant, so that decision is better left to the user.
As a workaround, I'd suggest disabling flake8's line length rules. Black should be responsible for formatting, and you don't get much additional value out of another tool complaining about formatting minutiae.
I'll bring this up with the team, thanks.