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

Strings should remain on separate lines if they end with “\n” #4570

Closed
15r10nk opened this issue Jan 31, 2025 · 3 comments
Closed

Strings should remain on separate lines if they end with “\n” #4570

15r10nk opened this issue Jan 31, 2025 · 3 comments
Labels
F: strings Related to our handling of strings T: style What do we want Blackened code to look like?

Comments

@15r10nk
Copy link

15r10nk commented Jan 31, 2025

Describe the style change

Strings should remain on separate lines if they end with “\n”. The newline has already the meaning of a line-break in a string. I think it would improve the readability if black uses this information to format strings.

Examples in the current Black style

a = "a=1\n" "b=2\n" "c=3\n"

Desired style

a = (
    "a=1\n"
    "b=2\n"
    "c=3\n"
)

Additional context

I'm the author of inline-snapshot and I use currently multi-line strings to generate strings with newlines, but it is not possible for black to indent theses strings.

from inline_snapshot import snapshot


def gen_code():
    return "\n".join(f"{name}={num}" for num, name in enumerate(("a", "b", "c")))


def test():
    assert gen_code() == snapshot(
        """\
a=0
b=1
c=2\
"""
    )

I would like to generate code like the following, but black merges the strings, which makes them hard to read.

from inline_snapshot import snapshot


def gen_code():
    return "\n".join(f"{name}={num}" for num, name in enumerate(("a", "b", "c")))


def test():
    assert gen_code() == snapshot(
        "a=0\n"
        "b=1\n"
        "c=2"
    )

> black

from inline_snapshot import snapshot


def gen_code():
    return "\n".join(f"{name}={num}" for num, name in enumerate(("a", "b", "c")))


def test():
    assert gen_code() == snapshot("a=0\n" "b=1\n" "c=2")

It would be nice if the strings can stay on separate lines if they end with newlines, or to have another way to generate multi-line strings which can be indented.

@15r10nk 15r10nk added the T: style What do we want Blackened code to look like? label Jan 31, 2025
@cobaltt7 cobaltt7 added the F: strings Related to our handling of strings label Jan 31, 2025
@XenoBytesX
Copy link

Can someone review this formating suggestion?
If ok, i can work on this and sumbit a pull request

@JelleZijlstra
Copy link
Collaborator

This makes sense to me, feel free to add a PR adding this to the preview style.

@JelleZijlstra
Copy link
Collaborator

Duplicate of #1540

@JelleZijlstra JelleZijlstra marked this as a duplicate of #1540 Jan 31, 2025
@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

4 participants