Skip to content
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

Black fail to format long strings and comments #1331

Closed
neoglez opened this issue Apr 3, 2020 · 12 comments
Closed

Black fail to format long strings and comments #1331

neoglez opened this issue Apr 3, 2020 · 12 comments
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: strings Related to our handling of strings S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like?

Comments

@neoglez
Copy link

neoglez commented Apr 3, 2020

Black 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.
@neoglez neoglez changed the title Black fail to format log strings and comments Black fail to format long strings and comments Apr 3, 2020
@ichard26
Copy link
Collaborator

ichard26 commented May 3, 2020

@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
Copy link
Contributor

@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
Copy link

paride commented Sep 16, 2020

#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
Copy link
Collaborator

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

@ichard26 ichard26 added the T: style What do we want Blackened code to look like? label Sep 16, 2020
@paride
Copy link

paride commented Sep 16, 2020

@ichard26 thanks for the pointer!

@ichard26 ichard26 added F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: strings Related to our handling of strings S: accepted The changes in this design / enhancement issue have been accepted and can be implemented labels May 30, 2021
@ichard26
Copy link
Collaborator

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

@1Mark
Copy link

1Mark commented Aug 1, 2021

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
Copy link
Collaborator

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

@1Mark
Copy link

1Mark commented Aug 1, 2021

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
Copy link

kehh commented Mar 25, 2022

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
Copy link

lafolle commented May 17, 2022

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: strings Related to our handling of strings S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

8 participants