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

Bug hazard - multiple strings given same indentation #4249

Open
JamesHutchison opened this issue Feb 23, 2024 · 4 comments
Open

Bug hazard - multiple strings given same indentation #4249

JamesHutchison opened this issue Feb 23, 2024 · 4 comments
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. F: strings Related to our handling of strings T: style What do we want Blackened code to look like?

Comments

@JamesHutchison
Copy link

JamesHutchison commented Feb 23, 2024

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.

@JamesHutchison JamesHutchison added the T: style What do we want Blackened code to look like? label Feb 23, 2024
@JelleZijlstra
Copy link
Collaborator

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

JamesHutchison commented Feb 23, 2024

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.

@JelleZijlstra JelleZijlstra added F: strings Related to our handling of strings F: parentheses Too many parentheses, not enough parentheses, and so on. labels Feb 23, 2024
@Harshad-Yadav

This comment was marked as duplicate.

@Harshad-Yadav
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. F: strings Related to our handling of strings T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

3 participants