Skip to content

Avoid emitting multi-line f-string elements before Python 3.12#24377

Merged
charliermarsh merged 1 commit intomainfrom
charlie/formatter-multiline
Apr 5, 2026
Merged

Avoid emitting multi-line f-string elements before Python 3.12#24377
charliermarsh merged 1 commit intomainfrom
charlie/formatter-multiline

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

Summary

See: #24355 (comment). Prior to Python 3.12, we need to avoid emitting formatted expressions that span multiple lines in non-triple quoted f-strings.

@charliermarsh charliermarsh added bug Something isn't working formatter Related to the formatter labels Apr 2, 2026
@charliermarsh charliermarsh force-pushed the charlie/formatter-multiline branch from 1ac515a to 2af1535 Compare April 2, 2026 21:08
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 2, 2026

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

```python
if f"aaaaaaaaaaa {[ttttteeeeeeeeest]} more {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
}":
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually invalid on Python 3.11, but the input is already invalid in this case, so I think it's within the bounds of the formatter contract?

@charliermarsh charliermarsh marked this pull request as ready for review April 2, 2026 21:09
@MichaReiser
Copy link
Copy Markdown
Member

I'll review on Tuesday. What's the underlying issue here. Is it that the formatter sometimes formats an interpolation over multiple lines if the f-string contains another multiline interpolation?

@charliermarsh charliermarsh force-pushed the charlie/parser-multiline branch from 03d8338 to da1acf5 Compare April 3, 2026 14:00
@charliermarsh charliermarsh force-pushed the charlie/formatter-multiline branch from 2af1535 to 215c43d Compare April 3, 2026 14:00
@charliermarsh
Copy link
Copy Markdown
Member Author

Is it that the formatter sometimes formats an interpolation over multiple lines if the f-string contains another multiline interpolation?

Yeah that's right. So I'm unclear if we actually consider this a bug, because I think the code has to be syntactically invalid already in order for this to trigger? In other words, we're introducing another syntax error, but the code was already invalid in another interpolation.

@MichaReiser
Copy link
Copy Markdown
Member

Yeah. I guess it does make it worse. I was mainly curious to understand how rigorous we need to be (e.g. can a triple quoted f-string that's nested in a single quoted f-string contain newlines?)

@charliermarsh
Copy link
Copy Markdown
Member Author

Thinking about it more, I'm sort of inclined to just treat this as a wontfix. I don't know that it's worth the time / concern / complexity.

@dylwil3
Copy link
Copy Markdown
Collaborator

dylwil3 commented Apr 3, 2026

This warning in the formatter tests seems to implicitly say it's okay to have syntax errors if they were also present in the source:

let sub = SubDiagnostic::new(
SubDiagnosticSeverity::Warning,
"Only accept new syntax errors if they are also present in the input. \
The formatter should not introduce syntax errors.",

So I think wontfix makes sense. (Are we even technically adding a new syntax error? Or just making the existing one "worse"?)

Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine either way and agree that I don't consider this a bug, but not introducing more syntax errors seems preferable, and the added complexity is limited.

Thank you

@charliermarsh charliermarsh force-pushed the charlie/parser-multiline branch 3 times, most recently from 3144fb7 to d2cd4d1 Compare April 5, 2026 15:04
Base automatically changed from charlie/parser-multiline to main April 5, 2026 15:09
@charliermarsh charliermarsh force-pushed the charlie/formatter-multiline branch from 215c43d to a24edfe Compare April 5, 2026 15:10
@charliermarsh charliermarsh enabled auto-merge (squash) April 5, 2026 15:10
@charliermarsh charliermarsh merged commit 1fe1c5f into main Apr 5, 2026
42 checks passed
@charliermarsh charliermarsh deleted the charlie/formatter-multiline branch April 5, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working formatter Related to the formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants