psf / black

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

Bug hazard - multiple strings given same indentation #4249

Open JamesHutchison opened 8 months ago

JamesHutchison commented 8 months ago

Describe the style change

Strings that are concatenated together are always enclosed in parenthesis.

Examples in the current Black style

some_func(
    "can you"
    "spot the bug",
    "in this list of strings",
    "where a comma is missing?"
)

Desired style

some_func(
    (
        "can you"
        "spot the bug"
    ),
    "in this list of strings",
    "where a comma is missing?"
)

Additional context

This isn't just a bug hazard where you accidentally join strings in an *args function. I frequently split strings that are too long by simply adding a "" at the split point and let black fix it. However, this doesn't format the way I want, so I have to spend time manually adding parenthesis around it.

Also, both styles are currently accepted by black. I'm suggesting the first one should be rejected. I would be happy if there was an option to do this, and would be even happier if its already an option and I'm just not aware of it :)

In typical usage, the bug is harder to spot because the strings are often longer and the comma is all the way on the right-side of the editor. When you have mixed styles where some of the joins are intentional, its even harder to spot the bug.

JelleZijlstra commented 8 months ago

What you describe isn't actually Black's current style.

It will put all the strings on line because they're short enough:

% black -c '''some_func(
    "can you"
    "spot the bug",
    "in this list of strings",
    "where a comma is missing?" 
)
'''          
some_func(
    "can you" "spot the bug", "in this list of strings", "where a comma is missing?"
)

If you use the magic trailing comma to force each argument on its own line, the two implicitly concatenated strings are kept together, making it fairly clear that they belong together:

% black -c '''some_func(
    "can you"
    "spot the bug",
    "in this list of strings",
    "where a comma is missing?",
)
'''
some_func(
    "can you" "spot the bug",
    "in this list of strings",
    "where a comma is missing?",
)

Your example reproduces only when the strings are too long for the line length:

% black -c '''some_func(
    "can you"
    "spot the bug",
    "in this list of strings",
    "where a comma is missing?",
)
''' -l 20           
some_func(
    "can you"
    "spot the bug",
    "in this list of strings",
    "where a comma is missing?",
)

Now, we previously implemented the behavior you request, but reverted it in #3640 for stability reasons. Might be worth revisiting though.

JamesHutchison commented 8 months ago

Yeah, I guess I deal with long lines a lot. The most recent use case where this came up was a bulleted list of sentences, so its much harder to notice when a bullet combines with a previous one.

Would happy if its just a config option. The fact its a bug hazard is why I am suggesting it just be the default behavior.

Harshad-Yadav commented 11 hours ago

curent black style

some_func( "can you" "spot the bug", "in this list of strings", "where a comma is missing?" )

Desired Style:

some_func( ( "can you" "spot the bug" ), "in this list of strings", "where a comma is missing?" )

Harshad-Yadav commented 7 hours ago

i am beginer in open source please give me simple code base issue so i can contribute effectively