microsoft / microsoft/pyright

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

Open
#11,514 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**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.

```python
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)
```

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied nested-comprehension sample through Pyright and compare its diagnostic with Python's SyntaxError. Trace the diagnostic path for the walrus operator and comprehension iterable, then verify that the reported error matches the valid surrounding parentheses and the actual invalid usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.