Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Recover incomplete list comprehension elements#25326

Merged
MichaReiser merged 1 commit into
mainfrom
micha/ty-2365-comprehension-completions
May 23, 2026
Merged

Recover incomplete list comprehension elements#25326
MichaReiser merged 1 commit into
mainfrom
micha/ty-2365-comprehension-completions

Conversation

@MichaReiser

@MichaReiser MichaReiser commented May 22, 2026

Copy link
Copy Markdown
Member

Summary

Fixes astral-sh/ty#2365.

The parser eats keywords if they appear in a name position. For example, the parser's error recovery eats the async keyword in:

a = async

because the user could be in the middle of ty ping asyncio.

However, this behavior can sometimes be too eager and result in worse recovery. For example, if we have:

for <CURSOR>in list:
	...

It's way more likely that the user is in the middle of typing the for target than that in is the intended target name. This PR extends the parser recovery by intentionally leaving certain keyword tokens in place, if they're expected in this context. The ideal parse tree for the above is a for statement with a missing target.

This is implemented by adding two flags to ExpressionContext that inform the identifier parsing to not eat a for or in keyword. This improves error recovery in comprehensions and for loops.

Test Plan

Added parser tests

@MichaReiser MichaReiser added the ty Multi-file analysis & type inference label May 22, 2026
@astral-sh-bot

astral-sh-bot Bot commented May 22, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser MichaReiser force-pushed the micha/ty-2365-comprehension-completions branch from 403a0cf to 6c6871a Compare May 22, 2026 16:57
@codspeed-hq

codspeed-hq Bot commented May 22, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 123 untouched benchmarks


Comparing micha/ty-2365-comprehension-completions (7dd9401) with main (8e420ea)

Open in CodSpeed

@MichaReiser MichaReiser force-pushed the micha/ty-2365-comprehension-completions branch 2 times, most recently from ec5db73 to 97387e5 Compare May 22, 2026 17:03
@MichaReiser MichaReiser added parser Related to the parser server Related to the LSP server and removed parser Related to the parser server Related to the LSP server labels May 22, 2026
@MichaReiser MichaReiser changed the title [ty] Recover incomplete list comprehension elements Recover incomplete list comprehension elements May 22, 2026
@MichaReiser MichaReiser force-pushed the micha/ty-2365-comprehension-completions branch 3 times, most recently from a65d357 to d9dd070 Compare May 22, 2026 18:24
@MichaReiser MichaReiser marked this pull request as ready for review May 22, 2026 18:24
@astral-sh-bot astral-sh-bot Bot requested a review from charliermarsh May 22, 2026 18:24
Comment thread crates/ruff_python_parser/src/parser/expression.rs
@MichaReiser MichaReiser force-pushed the micha/ty-2365-comprehension-completions branch from d9dd070 to 7dd9401 Compare May 23, 2026 08:19
@MichaReiser MichaReiser merged commit 5f284d1 into main May 23, 2026
52 checks passed
@MichaReiser MichaReiser deleted the micha/ty-2365-comprehension-completions branch May 23, 2026 09:03
thejchap pushed a commit to thejchap/ruff that referenced this pull request May 23, 2026
anishgirianish pushed a commit to anishgirianish/ruff that referenced this pull request May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Completions for [item.<CURSOR> for item in xs] fail because of how the comprehension is parsed

2 participants