python / python/cpython

Incorrect SyntaxError location for walrus operator in multi-line conditional expression

Open
#146,260 16 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core topic-parser type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

When using the walrus operator (:=) inside a multi-line conditional expression, Python raises a SyntaxError with the error position pointing to the wrong location.
The caret (^) is shown under the expression body instead of the if clause where the syntax issue actually occurs, making debugging misleading.
The error indicator should instead highlight the if portion (or walrus usage context), not the preceding expression line.

Code
(
    1
    if x := True
    else 2
)
Traceback
File "[REDACTED]", line 2
    1
    ^
SyntaxError: expected 'else' after 'if' expression

It locates error to 1 but it should have located to if x := True (line no. 3)

It does not errors when condition is parenthesized:

(
    1
    if (x := True)
    else 2
)  # output: 1
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-146305

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the multiline conditional expression and its parenthesized variant on CPython 3.14, then trace the parser or syntax-error location handling for the walrus expression. Done means the caret points to the if x := True line while the parenthesized case continues to parse and report its result correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.