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

Avoid multiline expression if format specifier is present #11123

Merged
merged 2 commits into from
Apr 26, 2024

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Apr 24, 2024

Summary

This PR fixes the bug where the formatter would format an f-string and could potentially change the AST.

For a triple-quoted f-string, the element can't be formatted into multiline if it has a format specifier because otherwise the newline would be treated as part of the format specifier.

Given the following f-string:

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
    variable:.3f} ddddddddddddddd eeeeeeee"""

The formatter sees that the f-string is already multiline so it assumes that it can contain line breaks i.e., broken into multiple lines. But, in this specific case we can't format it as:

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
    variable:.3f
} ddddddddddddddd eeeeeeee"""

Because the format specifier string would become ".3f\n", which is not the original string (.3f).

If the original source code already contained a newline, they'll be preserved. For example:

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
    variable:.3f
} ddddddddddddddd eeeeeeee"""

The above will be formatted as:

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable:.3f
} ddddddddddddddd eeeeeeee"""

Note that the newline after .3f is part of the format specifier which needs to be preserved.

The Python version is irrelevant in this case.

fixes: #10040

Test Plan

Add some test cases to verify this behavior.

@dhruvmanila dhruvmanila added bug Something isn't working formatter Related to the formatter labels Apr 24, 2024
Copy link
Contributor

github-actions bot commented Apr 24, 2024

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Comment on lines -208 to +257
bracket_spacing.fmt(f)
if conversion.is_none() && format_spec.is_none() {
bracket_spacing.fmt(f)?;
}

Ok(())
Copy link
Member Author

Choose a reason for hiding this comment

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

This is an additional change which isn't relevant to this PR but should be a follow-up. I've included it in here for simplicity. For example,

x = f"aaaaaaaaaaaaaaa {
    {'aaaaaaaaaaaaaaaaaa', 'dddddddddddddddddd'}!s
}"

This should be formatted as:

x = f"aaaaaaaaaaaaaaa { {'aaaaaaaaaaaaaaaaaa', 'dddddddddddddddddd'}!s}"

Instead of:

x = f"aaaaaaaaaaaaaaa { {'aaaaaaaaaaaaaaaaaa', 'dddddddddddddddddd'}!s }"
#                                                                     ^ this isn't required if there's format spec or conversion spec

@dhruvmanila dhruvmanila marked this pull request as ready for review April 25, 2024 10:15
@MichaReiser
Copy link
Member

I'm confused, you say:

Note that the newline after .3f is part of the format specifier which needs to be preserved.

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable:.3f
} ddddddddddddddd eeeeeeee"""

But the original example had no newline after 3f.

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
    variable:.3f} ddddddddddddddd eeeeeeee"""

} cccccccccc"""
aaaaaaaaaaa = f"""asaaaaaaaaaaaaaaaa {
aaaaaaaaaaaa + bbbbbbbbbbbb + ccccccccccccccc + dddddddd:.3f
# comment} cccccccccc"""
Copy link
Member

Choose a reason for hiding this comment

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

Lol what so # comment here is not a comment 🤣

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it isn't but only if it's a triple-quoted f-string

https://play.ruff.rs/b1d0326b-f8dc-4724-870f-db3770fb09c5

@dhruvmanila
Copy link
Member Author

I'm confused, you say:

Note that the newline after .3f is part of the format specifier which needs to be preserved.

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable:.3f
} ddddddddddddddd eeeeeeee"""

But the original example had no newline after 3f.

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
    variable:.3f} ddddddddddddddd eeeeeeee"""

The note is for the two snippets above it. The two code snippets you've provided aren't connected. I meant that the following code

f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
    variable:.3f
} ddddddddddddddd eeeeeeee"""

will be formatted as

-f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
-    variable:.3f
+f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {variable:.3f
 } ddddddddddddddd eeeeeeee"""

The formatter collapsed the f-string expression element but there's still a newline in the format spec which is why you see that the f-string is still multiline.

@dhruvmanila dhruvmanila enabled auto-merge (squash) April 26, 2024 13:16
@dhruvmanila dhruvmanila enabled auto-merge (squash) April 26, 2024 13:27
@dhruvmanila dhruvmanila merged commit 77a72ec into main Apr 26, 2024
18 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/f-string-format-spec branch April 26, 2024 13:34
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.

Update line break heuristic when f-string has format specifier
2 participants