ruff/crates
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
..
ruff Bump 0.14.0 (#20751) 2025-10-07 11:05:47 -07:00
ruff_annotate_snippets Display diffs for ruff format --check and add support for different output formats (#20443) 2025-09-30 12:00:51 -04:00
ruff_benchmark Shard ty walltime benchmarks (#20791) 2025-10-10 07:55:50 +02:00
ruff_cache
ruff_db Render unsupported syntax errors in formatter tests (#20777) 2025-10-13 10:00:37 -04:00
ruff_dev [ty] Document when a rule was added (#20859) 2025-10-14 14:33:48 +02:00
ruff_diagnostics Fix rust feature activation (#20012) 2025-08-21 09:26:06 +02:00
ruff_formatter Display diffs for ruff format --check and add support for different output formats (#20443) 2025-09-30 12:00:51 -04:00
ruff_graph [ty] Remove KnownModule::is_enum (#19681) 2025-08-01 10:31:12 +02:00
ruff_index
ruff_linter Improved error recovery for unclosed strings (including f- and t-strings) (#20848) 2025-10-15 09:50:56 +02:00
ruff_macros Replace two more uses of unsafe with const Option::unwrap (#20584) 2025-09-25 15:35:13 -04:00
ruff_memory_usage [ty] Clean up inherited generic contexts (#20647) 2025-10-03 13:55:43 -04:00
ruff_notebook Display diffs for ruff format --check and add support for different output formats (#20443) 2025-09-30 12:00:51 -04:00
ruff_options_metadata Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_ast Improved error recovery for unclosed strings (including f- and t-strings) (#20848) 2025-10-15 09:50:56 +02:00
ruff_python_ast_integration_tests Disallow implicit concatenation of t-strings and other string types (#19485) 2025-07-27 12:41:03 +00:00
ruff_python_codegen Generator preferred quote style (#20434) 2025-09-18 12:57:21 +02:00
ruff_python_formatter Fix syntax error false positives for escapes and quotes in f-strings (#20867) 2025-10-15 09:23:16 -04:00
ruff_python_importer [ruff] Add API for splicing into an existing import statement 2025-09-17 13:59:28 -04:00
ruff_python_index Track t-strings and f-strings for token-based rules and suppression comments (#20357) 2025-09-12 13:00:12 -05:00
ruff_python_literal
ruff_python_parser Fix syntax error false positives for escapes and quotes in f-strings (#20867) 2025-10-15 09:23:16 -04:00
ruff_python_semantic [ruff] Extend FA102 with listed PEP 585-compatible APIs (#20659) 2025-10-03 09:45:32 -04:00
ruff_python_stdlib [ruff] Extend FA102 with listed PEP 585-compatible APIs (#20659) 2025-10-03 09:45:32 -04:00
ruff_python_trivia Handle t-string prefixes in SimpleTokenizer (#20578) 2025-09-25 14:33:37 -05:00
ruff_python_trivia_integration_tests Handle t-string prefixes in SimpleTokenizer (#20578) 2025-09-25 14:33:37 -05:00
ruff_server Use Annotation::tags instead of hardcoded rule matching in ruff server (#20565) 2025-09-26 09:06:26 +02:00
ruff_source_file Move diff rendering to ruff_db (#20006) 2025-08-21 09:47:00 -04:00
ruff_text_size [ruff] Add TextRange::to_std_range 2025-09-17 13:59:28 -04:00
ruff_wasm Bump 0.14.0 (#20751) 2025-10-07 11:05:47 -07:00
ruff_workspace Update lint.flake8-type-checking.quoted-annotations docs (#20765) 2025-10-14 06:43:24 +00:00
ty [ty] Document when a rule was added (#20859) 2025-10-14 14:33:48 +02:00
ty_combine [ty] Disallow std::env and io methods in most ty crates (#20046) 2025-08-22 11:13:47 -07:00
ty_completion_eval [ty] Add some completion ranking improvements (#20807) 2025-10-15 08:59:33 +00:00
ty_ide [ty] Sync vendored typeshed stubs (#20876) 2025-10-15 11:13:32 +02:00
ty_project [ty] Use 3.14 as the default version (#20759) 2025-10-08 11:38:47 +02:00
ty_python_semantic [ty] Add (unused) inferable parameter to type property methods (#20865) 2025-10-15 09:05:15 -04:00
ty_server [ty] Add some completion ranking improvements (#20807) 2025-10-15 08:59:33 +00:00
ty_static [ty] improve base conda distinction from child conda (#20675) 2025-10-03 13:56:06 +00:00
ty_test [ty] Filter out revealed-type and undefined-reveal diagnostics from mdtest snapshots (#20820) 2025-10-12 18:39:32 +00:00
ty_vendored [ty] Sync vendored typeshed stubs (#20876) 2025-10-15 11:13:32 +02:00
ty_wasm [ruff,ty] Enable tracing's log feature 2025-10-03 08:18:03 -04:00