Skip to content

Incorrect error message for an invalid usage of the walrus operator in a nested comprehension #11514

Description

@MetRonnie

Describe the bug
Incorrect error message for an invalid usage of the walrus operator in a nested comprehension.

Code or Screenshots
If possible, provide a minimal, self-contained code sample (surrounded by triple back ticks) to demonstrate the issue. The code should define or import all referenced symbols.

print([
    f"{i=}"
    for i in [
        x
        for j in range(10)
        if (x := j * 2) % 3 == 0
    ]
])

Pylance gives:

Operator ":=" is not allowed in this context without surrounding parentheses

Note the expression is already surrounded by parentheses.

Whereas running the code gives:

SyntaxError: assignment expression cannot be used in a comprehension iterable expression

VS Code extension or command-line

VS Code:

Client: Pylance client (2026.2.1) started with python extension (2026.4.0)
Pylance language server 2026.2.1 (pyright version 1.1.408, commit 138238b4)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions