psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.82k stars 2.45k forks source link

Black fail to format long strings and comments #1331

Closed neoglez closed 3 years ago

neoglez commented 4 years ago

Black https://github.com/python/black/commit/9ed254

Playground link

Options

--line-length=88

Input


very_long_string = "123456789123456789E123456789asgfpjaspogjojasfogjoajfgojaofjgpaojfgdfkksdkhgspkgpk/k"

# Cordoba, Spain (CNN)The fortress town of Zahara de la Sierra in southern Spain is used to fending off enemies. The Moors and Christians fought over it in medieval times, and it was sacked by the French in 1812. Now its formidable position high above the Andalusian countryside has suddenly become an invaluable asset once more.
# On March 14, Zahara cut itself off from the outside world as a dangerous coronavirus spread its tentacles across Spain. The mayor, 40-year-old Santiago Galván, decided to block all but one of the town's five entrances. Galván acted the day that Spain's "state of alarm" came into force.

Output

very_long_string = "123456789123456789E123456789asgfpjaspogjojasfogjoajfgojaofjgpaojfgdfkksdkhgspkgpk/k"

# Cordoba, Spain (CNN)The fortress town of Zahara de la Sierra in southern Spain is used to fending off enemies. The Moors and Christians fought over it in medieval times, and it was sacked by the French in 1812. Now its formidable position high above the Andalusian countryside has suddenly become an invaluable asset once more.
# On March 14, Zahara cut itself off from the outside world as a dangerous coronavirus spread its tentacles across Spain. The mayor, 40-year-old Santiago Galván, decided to block all but one of the town's five entrances. Galván acted the day that Spain's "state of alarm" came into force.

Expected

Output

very_long_string = (
    "123456789123456789E123456789asgfpjaspogjojasfogjoajfg"
    "ojaofjgpaojfgdfkksdkhgspkgpk/k")

# Cordoba, Spain (CNN)The fortress town of Zahara de la Sierra in southern
# Spain is used to fending off enemies. The Moors and Christians fought over it
# in medieval times, and it was sacked by the French in 1812. Now its
# formidable position high above the Andalusian countryside has suddenly become
# an invaluable asset once more.
# On March 14, Zahara cut itself off from the outside world as a dangerous
# coronavirus spread its tentacles across Spain. The mayor, 40-year-old
# Santiago Galván, decided to block all but one of the town's five entrances.
# Galván acted the day that Spain's "state of alarm" came into force.
ichard26 commented 4 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.

bbugyi200 commented 4 years ago

@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.

paride commented 4 years ago

1132 has been merged but apparently black still doesn't wrap long lines, even if they contain spaces. I even tried with the very same example given in #1132's description and it doesn't work:

$ 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
ichard26 commented 4 years ago

@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).

paride commented 4 years ago

@ichard26 thanks for the pointer!

ichard26 commented 3 years ago

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 :)

1Mark commented 3 years ago

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?

JelleZijlstra commented 3 years ago

It's experimental! We plan to turn it on by default in the future.

1Mark commented 3 years ago

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 :)

kehh commented 2 years ago

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

lafolle commented 2 years ago

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?

JelleZijlstra commented 2 years ago

The --preview flag will format long strings but not long comments. There are no current plans to split long comments.

evgenyzdanovich commented 5 months ago

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

glebret commented 4 months ago

Do you plan to strip comment line also ? Flake is raising error, Black doesn't allow us to solve them.

felixjichao commented 3 months ago

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)