Skip to content

string_processing: Substitutions are never put at the end of wrapped f-strings #4677

@mlcui-corp

Description

@mlcui-corp

Describe the bug

When a long f-string is wrapped / split due to exceeding the line limit, Black will never put a substitution at the end of a f-string, resulting in unexpected wrapping behaviour.

To Reproduce

Input:

print(
    f"short {1} loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong"
)

Output with --unstable:

print(
    "short"
    f" {1} loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong"
)

Playground link

Expected behavior

print(
    f"short {1}"
    " loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong"
)

Environment

Additional context

Adding any character after {1} results in the expected behaviour of the substitution being put in the first line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: unstable formattingFormatting changed on the second passF: stringsRelated to our handling of stringsT: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions