Dhruv Manilawala
77a72ecd38
Avoid multiline expression if format specifier is present ( #11123 )
...
## 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:
```python
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:
```python
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:
```python
f"""aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb ccccccccccc {
variable:.3f
} ddddddddddddddd eeeeeeee"""
```
The above will be formatted as:
```py
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.
2024-04-26 13:34:38 +00:00
..
2024-02-29 10:00:51 +01:00
2024-01-10 12:09:34 +00:00
2023-11-30 00:09:55 +00:00
2023-12-21 04:20:17 +00:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2024-02-29 10:00:51 +01:00
2023-11-30 21:49:28 -05:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2023-12-06 07:15:06 +00:00
2024-02-29 10:00:51 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2024-01-31 00:09:38 +05:30
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2024-03-13 02:34:23 +00:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2024-01-10 12:09:34 +00:00
2024-02-29 10:00:51 +01:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2024-01-10 12:09:34 +00:00
2024-02-29 10:00:51 +01:00
2024-02-29 10:00:51 +01:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2024-01-26 08:18:30 +01:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2023-12-13 03:43:23 +00:00
2023-11-30 21:49:28 -05:00
2024-01-04 19:39:37 -05:00
2024-02-29 10:00:51 +01:00
2023-12-19 00:43:20 -06:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2024-02-29 10:00:51 +01:00
2024-02-29 10:00:51 +01:00
2024-02-29 10:00:51 +01:00
2024-02-29 10:00:51 +01:00
2024-02-29 09:30:54 +01:00
2024-02-29 09:30:54 +01:00
2023-11-30 00:09:55 +00:00
2023-11-30 00:09:55 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2023-11-30 21:11:14 -05:00
2024-01-30 10:08:50 +00:00
2023-11-09 01:49:24 +00:00
2024-01-30 10:08:50 +00:00
2024-01-30 10:08:50 +00:00
2024-02-29 09:30:54 +01:00
2024-02-05 13:29:24 +00:00
2024-02-12 16:09:13 +01:00
2024-03-13 02:34:23 +00:00
2024-04-18 17:57:39 +05:30
2023-11-09 01:49:24 +00:00
2024-04-18 17:57:39 +05:30
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-17 12:24:19 -05:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-12-14 04:58:17 +00:00
2023-11-09 01:49:24 +00:00
2024-01-30 10:08:50 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-03-13 02:34:23 +00:00
2024-04-26 13:34:38 +00:00
2023-11-09 01:49:24 +00:00
2023-11-30 21:11:14 -05:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-04-18 17:57:39 +05:30
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2024-04-18 17:57:39 +05:30
2023-11-30 21:11:14 -05:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2024-03-20 20:30:22 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-01-30 10:08:50 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-01-30 10:08:50 +00:00
2023-11-09 01:49:24 +00:00
2024-01-30 10:08:50 +00:00
2023-11-09 01:49:24 +00:00
2024-03-13 02:34:23 +00:00
2023-11-09 01:49:24 +00:00
2024-03-13 02:34:23 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2024-03-13 02:34:23 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-01-04 19:39:37 -05:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-30 21:49:28 -05:00
2024-02-29 09:30:54 +01:00
2024-02-29 09:30:54 +01:00
2024-01-31 11:13:37 +01:00
2024-03-13 02:34:23 +00:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-02-02 17:56:25 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-02-02 17:56:25 +01:00
2024-01-31 11:13:37 +01:00
2024-02-02 17:56:25 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-31 11:13:37 +01:00
2024-01-30 10:08:50 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2024-03-13 02:34:23 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2023-12-03 19:15:40 -05:00
2023-11-09 01:49:24 +00:00
2024-04-26 09:47:29 +02:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2024-01-08 14:47:01 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-03-13 02:34:23 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-04-26 09:47:29 +02:00
2023-11-09 01:49:24 +00:00
2024-03-08 23:48:47 +00:00
2024-03-08 23:56:02 +00:00
2024-01-31 00:09:38 +05:30
2024-01-31 00:09:38 +05:30
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00
2024-02-29 09:30:54 +01:00
2023-11-09 01:49:24 +00:00
2024-01-30 10:08:50 +00:00
2023-11-09 01:49:24 +00:00
2023-11-09 01:49:24 +00:00