ruff/crates
Brent Westbrook 71f8389f61
Fix syntax error false positives on parenthesized context managers (#20846)
This PR resolves the issue noticed in
https://github.com/astral-sh/ruff/pull/20777#discussion_r2417233227.
Namely, cases like this were being flagged as syntax errors despite
being perfectly valid on Python 3.8:

```pycon
Python 3.8.20 (default, Oct  2 2024, 16:34:12)
[Clang 18.1.8 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with (open("foo.txt", "w")): ...
...
Ellipsis
>>> with (open("foo.txt", "w")) as f: print(f)
...
<_io.TextIOWrapper name='foo.txt' mode='w' encoding='UTF-8'>
```

The second of these was already allowed but not the first:

```shell
> ruff check --target-version py38 --ignore ALL - <<EOF
with (open("foo.txt", "w")): ...
with (open("foo.txt", "w")) as f: print(f)
EOF
invalid-syntax: Cannot use parentheses within a `with` statement on Python 3.8 (syntax was added in Python 3.9)
 --> -:1:6
  |
1 | with (open("foo.txt", "w")): ...
  |      ^
2 | with (open("foo.txt", "w")) as f: print(f)
  |

Found 1 error.
```

There was some discussion of related cases in
https://github.com/astral-sh/ruff/pull/16523#discussion_r1984657793, but
it seems I overlooked the single-element case when flagging tuples. As
suggested in the other thread, we can just check if there's more than
one element or a trailing comma, which will cause the tuple parsing on
<=3.8 and avoid the false positives.
2025-10-13 14:13:27 -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] Remove duplicate global lint registry (#20053) 2025-08-22 19:43:12 -04:00
ruff_diagnostics
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
ruff_index
ruff_linter Render unsupported syntax errors in formatter tests (#20777) 2025-10-13 10:00:37 -04: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
ruff_python_ast [ty] Use 3.14 as the default version (#20759) 2025-10-08 11:38:47 +02:00
ruff_python_ast_integration_tests
ruff_python_codegen Generator preferred quote style (#20434) 2025-09-18 12:57:21 +02:00
ruff_python_formatter Fix syntax error false positives on parenthesized context managers (#20846) 2025-10-13 14:13:27 -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 on parenthesized context managers (#20846) 2025-10-13 14:13:27 -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
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 default and latest Python versions for 3.14 (#20725) 2025-10-07 12:23:11 -04:00
ty [ty] Remove 'pre-release software' warning (#20817) 2025-10-13 19:50:19 +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 an evaluation for completions 2025-10-08 08:44:21 -04:00
ty_ide [ty] defer inference of legacy TypeVar bound/constraints/defaults (#20598) 2025-10-09 21:08:37 +00:00
ty_project [ty] Use 3.14 as the default version (#20759) 2025-10-08 11:38:47 +02:00
ty_python_semantic [ty] Treat functions, methods, and dynamic types as function-like Callables (#20842) 2025-10-13 15:21:55 +02:00
ty_server [ty] Improve diagnostics for bad @overload definitions (#20745) 2025-10-07 21:52:57 +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 (#20658) 2025-10-01 10:11:48 +02:00
ty_wasm [ruff,ty] Enable tracing's log feature 2025-10-03 08:18:03 -04:00