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

Add f-string formatting to the docs #15341

Merged
merged 6 commits into from
Jan 8, 2025
Merged

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Jan 8, 2025

Summary

This PR adds a new f-string formatting section to the documentation.

It adds it to the https://docs.astral.sh/ruff/formatter page and provides a backlink in the deviation section as well.

There isn't a great place to add this kind of section in our documentation as ideally it would go under "Ruff code style" (similar to Black code style) section.

Question

I've found having the "Quotes" and "Line breaks" section in structuring the content but I'm open to removing them. Any opinions are welcome!

There can be more content to it but I'm not sure if we want to provide a lot of details. For example, we can expand on the "Quotes" section to enumerate all additional cases where we preserve the quote style.

Preview

Screenshot 2025-01-08 at 9 26 39 PM

@dhruvmanila dhruvmanila added the documentation Improvements or additions to documentation label Jan 8, 2025
@dhruvmanila dhruvmanila marked this pull request as ready for review January 8, 2025 10:23
@MichaReiser
Copy link
Member

The section does feel a bit out of place. That makes me wonder if we should rename the Black compatibility section to Style guide, rephrase the section to say that we follow Black's style guide with a few exceptions and that this section documents where we go beyond what black does?

@dhruvmanila
Copy link
Member Author

That makes me wonder if we should rename the Black compatibility section to Style guide, rephrase the section to say that we follow Black's style guide with a few exceptions and that this section documents where we go beyond what black does?

Interesting. This seems like a good idea.

docs/formatter.md Outdated Show resolved Hide resolved
Comment on lines 232 to 233
By default, Ruff formats f-string expressions. This is a [known deviation](formatter/black.md#f-strings)
from Black, which does not format f-strings.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: By default suggests that there's a way to disable the behavior.

Suggested change
By default, Ruff formats f-string expressions. This is a [known deviation](formatter/black.md#f-strings)
from Black, which does not format f-strings.
Unlike Black, Ruff formats f-string expressions.

Copy link
Member

Choose a reason for hiding this comment

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

I think also it might be worth making clear exactly what we mean by "f-string expressions". I.e., I believe black will change f'{foo()}' to f"{foo()}" -- it just won't format the interpolated expressions inside the braces, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's correct. Good point, I'll make the change.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, kind of, it's complicated. Black does change the quotes but only if there are no nested expressions with quotes.

docs/formatter.md Outdated Show resolved Hide resolved
Comment on lines 235 to 240
Ruff employs several heuristics to determine how an f-string should be formatted, including the
following considerations:

* The quote style for both outer and nested f-strings
* Whether to break the f-string into multiple lines
* When to skip formatting the f-string altogether
Copy link
Member

Choose a reason for hiding this comment

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

I'm leaning toward removing those bullets because they're each covered by a sub-heading (except how to skip f-string formatting)

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, makes sense. I'll add the one for the last bullet point.

docs/formatter.md Outdated Show resolved Hide resolved
docs/formatter.md Outdated Show resolved Hide resolved
Comment on lines 274 to 280
Starting with Python 3.12 ([PEP 701](https://peps.python.org/pep-0701/)), f-string expressions can
span multiple lines. Ruff adopts a similar heuristic as [Prettier](https://prettier.io/docs/en/next/rationale.html#template-literals)
for when to introduce a line break in an f-string expression: it introduces line breaks only
if the original f-string expression already contains them.

For example, the following code:

Copy link
Member

Choose a reason for hiding this comment

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

I'm leaning towards motivating this closer to what Prettier wrote

Template literals can contain interpolations. Deciding whether it's appropriate to insert a linebreak within an interpolation unfortunately depends on the semantic content of the template - for example, introducing a linebreak in the middle of a natural-language sentence is usually undesirable. Since Prettier doesn't have enough information to make this decision itself, it uses a heuristic similar to that used for objects: it will only split an interpolation expression across multiple lines if there was already a linebreak within that interpolation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've provided a link to the relevant section in Prettier which is why I thought not to add the same detail here and keep it short.

Copy link
Member

Choose a reason for hiding this comment

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

Including a link to the source of our inspiration is great but I don't expect many users to click it and one important aspect of the style guide section is that we motivate our formatting. That's why I'd still prefer at least a few words why we trade consistency with flexibility.

docs/formatter.md Outdated Show resolved Hide resolved
@MichaReiser MichaReiser added this to the v0.9 milestone Jan 8, 2025
@dhruvmanila
Copy link
Member Author

@MichaReiser I've made some changes to introduce a "Style Guide" section. I've removed some content from previous section because it doesn't really fit in the new section. Happy to hear any thoughts on it, I can refine the section a bit more and add some more content if it looks good.

Copy link
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.

Nice. I really like where this is going. We might have to port some more deviations to the style guide section in the future but this is a great start.

One more nit and if we could motivate the line break behavior a bit more would be great.

docs/formatter.md Outdated Show resolved Hide resolved
Comment on lines 274 to 280
Starting with Python 3.12 ([PEP 701](https://peps.python.org/pep-0701/)), f-string expressions can
span multiple lines. Ruff adopts a similar heuristic as [Prettier](https://prettier.io/docs/en/next/rationale.html#template-literals)
for when to introduce a line break in an f-string expression: it introduces line breaks only
if the original f-string expression already contains them.

For example, the following code:

Copy link
Member

Choose a reason for hiding this comment

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

Including a link to the source of our inspiration is great but I don't expect many users to click it and one important aspect of the style guide section is that we motivate our formatting. That's why I'd still prefer at least a few words why we trade consistency with flexibility.

Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Looks great! A few grammatical nits

docs/formatter.md Outdated Show resolved Hide resolved
docs/formatter.md Outdated Show resolved Hide resolved
docs/formatter.md Outdated Show resolved Hide resolved
docs/formatter.md Outdated Show resolved Hide resolved
docs/formatter.md Outdated Show resolved Hide resolved
docs/formatter.md Outdated Show resolved Hide resolved
@dhruvmanila dhruvmanila merged commit a1d3557 into ruff-0.9 Jan 8, 2025
20 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/f-string-docs branch January 8, 2025 17:10
@dhruvmanila dhruvmanila mentioned this pull request Jan 8, 2025
2 tasks
@dhruvmanila dhruvmanila restored the dhruv/f-string-docs branch January 9, 2025 05:15
@dhruvmanila dhruvmanila deleted the dhruv/f-string-docs branch January 9, 2025 05:16
@dhruvmanila dhruvmanila restored the dhruv/f-string-docs branch January 9, 2025 05:16
dhruvmanila added a commit that referenced this pull request Jan 9, 2025
Revive #15341 as it got removed
from the latest rebase in #15238.
MichaReiser pushed a commit that referenced this pull request Jan 9, 2025
Revive #15341 as it got removed
from the latest rebase in #15238.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants