ruff/crates/ruff_python_formatter/resources/test/fixtures/ruff
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
..
carriage_return Normalize '\r' in string literals to '\n' 2023-06-30 10:13:23 +02:00
expression Fix syntax error false positives for escapes and quotes in f-strings (#20867) 2025-10-15 09:23:16 -04:00
fmt_on_off Spellcheck & grammar (#10375) 2024-03-13 02:34:23 +00:00
fmt_skip Allow `# fmt: skip` with interspersed same-line comments (#9395) 2024-01-04 19:39:37 -05:00
parentheses Comments outside expression parentheses (#7873) 2023-10-19 09:24:11 +00:00
pattern Preserve tuple parentheses in case patterns (#18147) 2025-05-22 07:52:21 +02:00
range_formatting Ruff 2025 style guide (#13906) 2025-01-09 10:20:06 +01:00
statement Remove parentheses around multiple exception types on Python 3.14+ (#20768) 2025-10-14 11:17:45 -04:00
stub_files [formatter] Fix missing blank lines before decorated classes in .pyi files (#18888) 2025-06-24 16:25:44 +02:00
.editorconfig Fix `\r` and `\r\n` handling in t- and f-string debug texts (#18673) 2025-06-15 06:53:06 +01:00
blank_line_before_class_docstring.options.json Implement `no_blank_line_before_class_docstring` preview style (#9154) 2023-12-19 00:43:20 -06:00
blank_line_before_class_docstring.py Implement `no_blank_line_before_class_docstring` preview style (#9154) 2023-12-19 00:43:20 -06:00
docstring.options.json Use double quotes for all docstrings, including single-quoted docstrings (#9020) 2023-12-07 04:41:00 +00:00
docstring.py Spellcheck & grammar (#10375) 2024-03-13 02:34:23 +00:00
docstring_chaperones.py Don't add chaperone space after escaped quote in triple quote (#17216) 2025-04-11 10:21:47 +02:00
docstring_code_examples.options.json ruff_python_formatter: implement "dynamic" line width mode for docstring code formatting (#9098) 2023-12-12 09:58:07 -05:00
docstring_code_examples.py Fix typos found by codespell (#9346) 2024-01-02 02:08:15 +00:00
docstring_code_examples_crlf.options.json format doctests in docstrings (#8811) 2023-11-27 11:14:55 -05:00
docstring_code_examples_crlf.py format doctests in docstrings (#8811) 2023-11-27 11:14:55 -05:00
docstring_code_examples_dynamic_line_width.options.json ruff_python_formatter: implement "dynamic" line width mode for docstring code formatting (#9098) 2023-12-12 09:58:07 -05:00
docstring_code_examples_dynamic_line_width.py Fix codeblock dynamic line length calculation for indented examples (#13523) 2024-09-27 09:09:07 +02:00
docstring_newlines.py Don't trim last empty line in docstrings (#9813) 2024-02-05 13:29:24 +00:00
docstring_non_visible_characters.py Fix typos found by codespell (#14863) 2024-12-09 09:32:12 +00:00
docstring_tab_indentation.options.json Docstring formatting: Preserve tab indentation when using `indent-style=tabs` (#9915) 2024-02-12 16:09:13 +01:00
docstring_tab_indentation.py Docstring formatting: Preserve tab indentation when using `indent-style=tabs` (#9915) 2024-02-12 16:09:13 +01:00
empty_multiple_trailing_newlines.py Fix handling of newlines in empty files (#7473) 2023-09-18 06:08:10 +00:00
empty_now_newline.py Fix handling of newlines in empty files (#7473) 2023-09-18 06:08:10 +00:00
empty_trailing_newline.py Fix handling of newlines in empty files (#7473) 2023-09-18 06:08:10 +00:00
empty_whitespace.py Fix handling of newlines in empty files (#7473) 2023-09-18 06:08:10 +00:00
f-string-carriage-return-newline.py Fix `\r` and `\r\n` handling in t- and f-string debug texts (#18673) 2025-06-15 06:53:06 +01:00
form_feed.py Treat form feed as whitespace in `SimpleTokenizer` (#7626) 2023-09-25 14:34:59 +00:00
module_dangling_comment1.py Format empty lines in stub files like black's preview style (#7206) 2023-09-11 08:03:59 +00:00
module_dangling_comment2.py Format empty lines in stub files like black's preview style (#7206) 2023-09-11 08:03:59 +00:00
multiline_string_deviations.py Hug multiline-strings preview style (#9243) 2024-01-10 12:47:34 +01:00
newlines.py Insert empty line between suite and alternative branch after def/class (#12294) 2024-07-15 12:59:33 +02:00
newlines.pyi Insert empty line between suite and alternative branch after def/class (#12294) 2024-07-15 12:59:33 +02:00
notebook_docstring.options.json Disable top-level docstring formatting for notebooks (#9957) 2024-02-12 18:14:02 +00:00
notebook_docstring.py Disable top-level docstring formatting for notebooks (#9957) 2024-02-12 18:14:02 +00:00
pattern_match_regression_brackets.py Preserve tuple parentheses in case patterns (#18147) 2025-05-22 07:52:21 +02:00
preview.options.json Add test and basic implementation for formatter preview mode (#8044) 2023-10-26 15:33:26 +00:00
preview.py Add test and basic implementation for formatter preview mode (#8044) 2023-10-26 15:33:26 +00:00
quote_style.options.json Add "preserve" quote-style to mimic Black's skip-string-normalization (#8822) 2023-12-07 23:59:22 +00:00
quote_style.py Preview Style: Format module level docstring (#9725) 2024-02-05 15:03:34 +00:00
skip_magic_trailing_comma.options.json Add tests for skip magic trailing comma 2023-06-26 14:15:55 +02:00
skip_magic_trailing_comma.py Improve `with` statement comment handling and expression breaking (#6621) 2023-08-18 03:30:38 +00:00
tab_width.options.json Introduce `IndentWidth` (#7301) 2023-09-13 14:52:24 +02:00
tab_width.py Introduce `IndentWidth` (#7301) 2023-09-13 14:52:24 +02:00
trailing_comments.py Treat `ty: ` comments as pragma comments (#18532) 2025-06-07 16:02:43 +02:00
trivia.py Use `pass` over ellipsis in non-function/class contexts (#8049) 2023-10-19 11:11:17 +02:00