Brent Westbrook
8b9ab48ac6
Fix syntax error false positives for escapes and quotes in f-strings ( #20867 )
...
Summary
--
Fixes #20844 by refining the unsupported syntax error check for [PEP
701]
f-strings before Python 3.12 to allow backslash escapes and escaped
outer quotes
in the format spec part of f-strings. These are only disallowed within
the
f-string expression part on earlier versions. Using the examples from
the PR:
```pycon
>>> f"{1:\x64}"
'1'
>>> f"{1:\"d\"}"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier '"d"' for object of type 'int'
```
Note that the second case is a runtime error, but this is actually
avoidable if
you override `__format__`, so despite being pretty weird, this could
actually be
a valid use case.
```pycon
>>> class C:
... def __format__(*args, **kwargs): return "<C>"
...
>>> f"{C():\"d\"}"
'<C>'
```
At first I thought narrowing the range we check to exclude the format
spec would
only work for escapes, but it turns out that cases like `f"{1:""}"` are
already
covered by an existing `ParseError`, so we can just narrow the range of
both our
escape and quote checks.
Our comment check also seems to be working correctly because it's based
on the
actual tokens. A case like
[this](https://play.ruff.rs/9f1c2ff2-cd8e-4ad7-9f40-56c0a524209f ):
```python
f"""{1:# }"""
```
doesn't include a comment token, instead the `#` is part of an
`InterpolatedStringLiteralElement`.
Test Plan
--
New inline parser tests
[PEP 701]: https://peps.python.org/pep-0701/
2025-10-15 09:23:16 -04:00
..
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-05-07 14:11:35 -04:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-04-30 15:04:00 -04:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-04-30 15:04:00 -04:00
2024-06-03 18:23:50 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-19 16:46:15 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-03-05 13:19:09 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-06-17 06:47:00 +00:00
2024-06-17 06:47:00 +00:00
2024-06-17 06:47:00 +00:00
2024-06-17 06:47:00 +00:00
2024-04-18 17:57:39 +05:30
2025-03-29 12:07:20 -04:00
2025-03-29 12:07:20 -04:00
2025-03-29 12:07:20 -04:00
2025-03-29 12:07:20 -04:00
2025-03-29 12:07:20 -04:00
2025-03-29 12:07:20 -04:00
2025-03-29 12:07:20 -04:00
2025-03-17 11:17:27 +00:00
2025-03-17 11:17:27 +00:00
2025-03-05 17:08:18 +00:00
2025-03-17 11:17:27 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-03-05 17:08:18 +00:00
2025-03-17 11:17:27 +00:00
2024-04-18 17:57:39 +05:30
2025-03-29 12:07:20 -04:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-04-03 17:55:37 -04:00
2025-04-03 17:55:37 -04:00
2025-03-21 15:06:22 -04:00
2025-03-04 16:50:09 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-04-29 07:55:30 -05:00
2025-04-29 07:55:30 -05:00
2025-06-16 11:44:42 -05:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-03-13 15:55:17 -04:00
2024-04-18 17:57:39 +05:30
2024-04-23 04:19:05 +00:00
2024-04-23 04:19:05 +00:00
2024-04-23 04:19:05 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-03-05 13:19:09 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-09-16 15:53:28 -04:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-04-08 08:55:43 -04:00
2025-04-08 08:55:43 -04:00
2025-04-08 08:55:43 -04:00
2025-04-08 08:56:25 -04:00
2025-04-08 08:55:43 -04:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-09-25 13:52:24 -04:00
2024-04-18 17:57:39 +05:30
2025-03-26 12:27:16 -04:00
2025-03-06 11:57:20 -05:00
2025-03-06 11:57:20 -05:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-02-27 10:23:15 -05:00
2024-06-03 18:23:50 +05:30
2024-06-03 18:23:50 +05:30
2024-06-03 18:23:50 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-06-03 18:23:50 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-04-03 17:32:39 -04:00
2024-04-18 17:57:39 +05:30
2025-09-24 19:32:55 +00:00
2025-03-14 15:06:42 -04:00
2025-03-14 15:06:42 -04:00
2025-04-08 12:50:52 -04:00
2025-10-15 09:23:16 -04:00
2024-04-19 11:36:26 +00:00
2025-04-24 16:11:46 -04:00
2025-04-24 16:11:46 -04:00
2025-04-24 16:11:46 -04:00
2025-04-24 16:11:46 -04:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-03-14 15:20:44 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-03-17 08:54:55 -04:00
2025-03-06 12:18:13 -05:00
2025-03-18 11:12:15 -04:00
2025-03-05 13:46:43 +00:00
2024-12-19 13:36:32 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-03-21 14:45:25 -04:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-04-02 08:38:25 -04:00
2025-04-02 08:38:25 -04:00
2025-04-02 08:38:25 -04:00
2025-03-29 12:35:47 -04:00
2025-03-14 14:51:34 +00:00
2025-03-14 14:51:34 +00:00
2025-05-30 15:00:56 -05:00
2025-05-30 15:00:56 -05:00
2025-05-30 15:00:56 -05:00
2025-05-30 15:00:56 -05:00
2025-05-30 15:00:56 -05:00
2025-05-30 15:00:56 -05:00
2025-05-30 15:00:56 -05:00
2025-07-27 12:41:03 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-12-10 17:50:55 -06:00
2025-10-13 14:13:27 -04:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2025-03-04 16:53:38 +00:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-26 09:47:29 +02:00
2024-04-26 09:47:29 +02:00
2024-04-26 09:47:29 +02:00
2024-04-26 09:47:29 +02:00
2024-04-18 17:57:39 +05:30
2024-04-26 09:47:29 +02:00
2024-04-26 09:47:29 +02:00
2024-06-26 08:10:35 +05:30
2025-03-04 17:20:10 +00:00
2025-03-14 15:06:42 -04:00
2025-03-14 15:06:42 -04:00
2025-03-14 15:06:42 -04:00
2024-04-18 17:57:39 +05:30
2025-02-28 17:13:46 -05:00
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-04-18 17:57:39 +05:30
2024-06-06 08:59:56 +00:00
2024-04-18 17:57:39 +05:30
2025-03-29 12:07:20 -04:00
2025-09-03 10:13:05 -04:00