Skip to content

Commit

Permalink
Expand line breaks section
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jan 9, 2025
1 parent 4031d3c commit 5b60afd
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions docs/formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ below.
#### Quotes

Ruff will use the configured quote style for the f-string expression unless doing so would result in
invalid syntax for the target Python version or it results in more backslashes than the original
invalid syntax for the target Python version or requires more backslash escapes than the original
expression. Specifically, Ruff will preserve the original quote style for the following cases:

When the target Python version is < 3.12 and a [self-documenting f-string expressions] contains a string
Expand Down Expand Up @@ -453,9 +453,12 @@ f"outer f-string {f'nested f-string {f"another nested f-string"} end'} end"
#### Line breaks

Starting with Python 3.12 ([PEP 701](https://peps.python.org/pep-0701/)), expression parts of an f-string 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 there was already a line break within any of the expression parts of an f-string.
span multiple lines. Ruff needs to decide when to introduce a line break in an f-string expression.
This depends on the semantic content of the expression parts of an f-string - for example,
introducing a line break in the middle of a natural-language sentence is undesirable. Since Ruff
doesn't have enough information to make that decision, it uses a heuristic similar to [Prettier](https://prettier.io/docs/en/next/rationale.html#template-literals):
it will only split the expression parts of an f-string across multiple lines if there was already a line break
within any of the expression parts.

For example, the following code:

Expand Down Expand Up @@ -485,7 +488,7 @@ f"this f-string has a multiline expression {['red', 'green', 'blue', 'yellow']}
```

If you want Ruff to split an f-string across multiple lines, ensure there's a linebreak somewhere within the
`{...}` expression.
`{...}` parts of an f-string.

[self-documenting f-string expressions]: https://realpython.com/python-f-strings/#self-documenting-expressions-for-debugging

Expand Down

0 comments on commit 5b60afd

Please sign in to comment.