Skip to content

format produces sematically incorrect output when lambda body contains implicit string concatenation #23851

@jamestrew

Description

@jamestrew

Summary

When a lambda contains a implicitly concatenated string where the string is a format string that takes lambda's param as the format arg, if the line is too long, ruff format will split the string across multiple lines without parenthesizing the string, turning the string into 2 separate strings, breaking the format string.

# Input
foo = lambda x: 'hello this is a really long string that then get concatenated ' 'with another string that prints {x!r}'.format(x=x)

# Ruff
foo = lambda x: "hello this is a really long string that then get concatenated "
"with another string that prints {x!r}".format(x=x)
                                               # ^ NameError: name 'x' is not defined

# Black
foo = (
    lambda x: "hello this is a really long string that then get concatenated "
    "with another string that prints {x!r}".format(x=x)
)

https://play.ruff.rs/160f41fc-c9ae-4422-8024-327f48bc2652

I was able to bisect the bug and found the regression begins at b534607

Friendly ping @dylwil3

Version

ruff 0.15.5+172 (fd9568a 2026-03-09)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingformatterRelated to the formatter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions