Skip to content

[parser] Fix indentation tracking after line continuations#23417

Merged
dhruvmanila merged 4 commits intomainfrom
fix/issue-19301
Feb 19, 2026
Merged

[parser] Fix indentation tracking after line continuations#23417
dhruvmanila merged 4 commits intomainfrom
fix/issue-19301

Conversation

@dhruvmanila
Copy link
Member

Summary

fixes: #19301

Test Plan

Add new lexer and parser test cases.

kar-ganap and others added 3 commits February 18, 2026 08:33
Per the Python spec, the whitespace up to the first backslash determines
the line's indentation, and continuation lines should not affect the
indentation level. The lexer's `eat_indentation` loop was not handling
this: after a `\` continuation, it accumulated the continuation line's
whitespace into the indentation, causing spurious IndentationError and
"Expected a statement" errors.

Fix: after consuming `\<newline>` during indentation tracking, skip the
continuation line's whitespace with `eat_while(is_python_whitespace)`
without accumulating it into `indentation`. Guard: only skip when
`indentation != Indentation::root()` — when `\` is at column 0, let the
loop continue so the next line's whitespace is accumulated normally.

Lexer snapshot tests cover: basic continuation in indented block,
continuation at column 0, multiple consecutive continuations, and
mismatched indentation error — each with all three EOL types.

Ref: https://docs.python.org/3/reference/lexical_analysis.html#indentation
See: python/cpython#90249
@dhruvmanila dhruvmanila added bug Something isn't working parser Related to the parser labels Feb 19, 2026
@dhruvmanila dhruvmanila enabled auto-merge (squash) February 19, 2026 12:47
@dhruvmanila dhruvmanila merged commit 6551438 into main Feb 19, 2026
45 checks passed
@dhruvmanila dhruvmanila deleted the fix/issue-19301 branch February 19, 2026 12:52
knutwannheden pushed a commit to openrewrite/ruff that referenced this pull request Feb 20, 2026
…#23417)

## Summary

fixes: astral-sh#19301

## Test Plan

Add new lexer and parser test cases.

---------

Co-authored-by: Kartik Ganapathi <kartikganapathi@icloud.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Syntax errors related to indentation after backslashes

2 participants