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

Open
15r10nk opened this issue Jan 31, 2025 · 0 comments
Open

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

15r10nk opened this issue Jan 31, 2025 · 0 comments
Labels
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

1 participant