Closed neoglez closed 3 years ago
@neoglez Black currently doesn't wrap long string literals or merge string literals that happen to be on the same line. There is an open issue regarding this: #182. I would also have to guess Black doesn't wrap long comments for the same reason for strings. It would require modifying the AST which isn't 100% safe and has a bunch of edge cases to be dealt with. Although it should be mentioned that there is a pull request for better string handling, it is #1132. Except, it is a very large PR and probably will take a lot of time to become merged.
Just a 'in case you didn't know' since your issue appears to be a bug report IMO.
@neoglez It should be noted that #1132 will not change the output of the example that you gave above since the example string has no spaces in it. This would be undesirable, for example, in the case of long URLs.
$ cat test.py
#!/usr/bin/python3
fstring = f"f-strings definitely make things more {difficult} than they need to be for {{black}}. But boy they sure are handy. The problem is that some lines will need to have the 'f' whereas others do not. This {line}, for example, needs one."
$ black test.py
All done! ✨ 🍰 ✨
1 file left unchanged.
$ black --version
black, version 20.8b2.dev23+g811decd
@paride the feature was unstable and causing a bunch of crashes so the decision was made to lock the feature under a hidden flag (PR https://github.com/psf/black/pull/1609).
@ichard26 thanks for the pointer!
Closing since 1) the --experimental-string-processing
flag has landed which handles the string part of this issue, 2) making it the default is tracked separated in https://github.com/psf/black/issues/2188, and 3) the comments case in tracked specifically in https://github.com/psf/black/issues/1713.
Thanks for the suggestions, at least one of them got accepted :)
Closing since 1) the
--experimental-string-processing
flag has landed which handles the string part of this issue, 2) making it the default is tracked separated in #2188, and 3) the comments case in tracked specifically in #1713.Thanks for the suggestions, at least one of them got accepted :)
@ichard26 Why isn't --experimental-string-processing
mentioned when you run black --help
?
It's experimental! We plan to turn it on by default in the future.
It's experimental! We plan to turn it on by default in the future.
git has loads of experimental features, they are still mentioned in the cli docs. How else are people supposed to try them so you can receive more testing + feedback :)
With the latest version of black (22.1) this is now enabled by using the --preview
flag on black:
https://black.readthedocs.io/en/stable/change_log.html#highlights
Hi there,
i'm still unable to format long comments with the --preview
flag.
version info- black, 22.3.0 (compiled: yes)
Test file:
def boom() -> None:
"""There’s an increasing regulatory and consumer pressure on companies to do a better job protecting sensitive customer data. Yet, despite this pressure, data breaches and compliance issues continue to plague the tech industry."""
# There’s an increasing regulatory and consumer pressure on companies to do a better job protecting sensitive customer data. Yet, despite this pressure, data breaches and compliance issues continue to plague the tech industry
pass
Command output:
root@ca9009d07c58:/opt/# black -v --line-length 80 --preview test_line.py
Identified `/opt/` as project root containing a .git directory.
Sources to be formatted: "test_line.py"
Using configuration from project root.
test_line.py wasn't modified on disk since last run.
All done! ✨ 🍰 ✨
1 file left unchanged.
Am I missing something? My understanding is that the functionality is protected behind --preview
flag -> hence with the flag the comments should still be formatted?
The --preview
flag will format long strings but not long comments. There are no current plans to split long comments.
JFYI, the issue is still in-place for me:
black --preview --line-length=120 test_transaction.py
does not split long string fields.
my versions are: black, 23.7.0 (compiled: yes) Python (CPython) 3.10.12
Do you plan to strip comment line also ? Flake is raising error, Black doesn't allow us to solve them.
From Version 24.1.0,
--preview --enable-unstable-feature string_processing
https://black.readthedocs.io/en/stable/change_log.html#id21 Remove the long-deprecated --experimental-string-processing flag. This feature can currently be enabled with --preview --enable-unstable-feature string_processing. (#4096)
Black https://github.com/python/black/commit/9ed254
Playground link
Options
--line-length=88
Input
Output
Expected
Output