ruff/crates/ruff_python_parser/src
Brent Westbrook 75a562d313
[syntax-errors] Parenthesized context managers before Python 3.9 (#16523)
Summary
--

I thought this was very complicated based on the comment here:
https://github.com/astral-sh/ruff/pull/16106#issuecomment-2653505671 and
on some of the discussion in the CPython issue here:
https://github.com/python/cpython/issues/56991. However, after a little
bit of experimentation, I think it boils down to this example:

```python
with (x as y): ...
```

The issue is parentheses around a `with` item with an `optional_var`, as
we (and
[Python](https://docs.python.org/3/library/ast.html#ast.withitem)) call
the trailing variable name (`y` in this case). It's not actually about
line breaks after all, except that line breaks are allowed in
parenthesized expressions, which explains the validity of cases like


```pycon
>>> with (
...     x,
...     y
... ) as foo:
...     pass
... 
```

even on Python 3.8.

I followed [pyright]'s example again here on the diagnostic range (just
the opening paren) and the wording of the error.


Test Plan
--
Inline tests

[pyright]:
https://pyright-play.net/?pythonVersion=3.7&strict=true&code=FAdwlgLgFgBAFAewA4FMB2cBEAzBCB0EAHhJgJQwCGAzjLgmQFwz6tA
2025-03-17 08:54:55 -04:00
..
lexer Mention that Cursor is based on rustc's implementation. (#12109) 2024-06-30 16:53:25 +01:00
parser [syntax-errors] Parenthesized context managers before Python 3.9 (#16523) 2025-03-17 08:54:55 -04:00
snapshots Update insta snapshots (#14366) 2024-11-15 19:31:15 +01:00
error.rs [syntax-errors] Parenthesized context managers before Python 3.9 (#16523) 2025-03-17 08:54:55 -04:00
lexer.rs bump MSRV to 1.83 (#16294) 2025-02-26 06:12:43 -08:00
lib.rs Document LinterResult::has_syntax_error and add Parsed::has_no_syntax_errors (#16443) 2025-03-04 08:35:38 -05:00
string.rs Upgrade to Rust 1.81 (#13265) 2024-09-06 15:09:09 +02:00
token.rs [pylint] Do not offer fix for raw strings (PLE251) (#16132) 2025-02-13 08:36:11 +00:00
token_set.rs Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
token_source.rs Use TokenSource to find new location for re-lexing (#12060) 2024-06-27 17:12:39 +05:30
typing.rs Rename ExprStringLiteral::as_unconcatenated_string() to ExprStringLiteral::as_single_part_string() (#16253) 2025-02-19 16:06:57 +00:00