Report a specific diagnostic for walrus in a comprehension iterable expression - #11606
Open
Henry Su (hsusul) wants to merge 1 commit into
Open
Report a specific diagnostic for walrus in a comprehension iterable expression#11606Henry Su (hsusul) wants to merge 1 commit into
Henry Su (hsusul) wants to merge 1 commit into
Conversation
A walrus operator used anywhere within a comprehension's iterable expression is a syntax error (PEP 572), and it cannot be fixed by adding parentheses. Pyright detected this but reused the generic "is not allowed in this context without surrounding parentheses" message, which is misleading here. Emit a dedicated message for this case while preserving the existing message for a bare walrus used as a comprehension "if" condition, where parentheses do resolve the error. Addresses microsoft#11514.
Collaborator
|
🔒 Automated review in progress — Stella Huang (@StellaHuang95) is auto-reviewing this PR. |
Stella Huang (StellaHuang95)
approved these changes
Aug 10, 2026
Stella Huang (StellaHuang95)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Rich Chiodo (rchiodo)
approved these changes
Aug 10, 2026
Rich Chiodo (rchiodo)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Rich Chiodo (rchiodo)
approved these changes
Aug 11, 2026
Rich Chiodo (rchiodo)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
Stella Huang (StellaHuang95)
approved these changes
Aug 13, 2026
Stella Huang (StellaHuang95)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
An assignment expression (
:=) used anywhere within the iterable expression of a comprehension'sforclause is a syntax error under PEP 572, and — unlike a walrus used bare as a comprehensionifcondition — it cannot be made legal by adding parentheses. Pyright already reports this as an error, but it reused the generic message:That message is misleading here: the offending walrus in the reproduction is already parenthesized, and no amount of parenthesization will fix it. This addresses #11514.
Minimal reproduction
CPython:
Current behavior
Pyright reports
Operator ":=" is not allowed in this context without surrounding parentheses, even though the walrus is parenthesized.Corrected behavior
Pyright reports
Operator ":=" is not allowed within a comprehension iterable expression.Root cause
In
parser.ts,_parseAssignmentExpressionemittedwalrusNotAllowedfor two distinct conditions that were OR'd together:These two flags actually represent different restrictions:
!this._assignmentExpressionsAllowedis set only by_disallowAssignmentExpression, whose only caller wraps parsing of a comprehensionfor ... in <iterable>sequence expression. This is the "walrus in a comprehension iterable" restriction, which parentheses do not resolve.disallowAssignmentExpressioncorresponds to a bare walrus that requires surrounding parentheses (e.g. an unparenthesized walrus used as a comprehensionifcondition, or a bare walrus where atestexpression is expected). Here parentheses do resolve the error, so the existing message is correct.Implementation
walrusNotAllowedInComprehension, while the parentheses case keepswalrusNotAllowed. The iterable case is checked first so it wins when both apply (parentheses would not help there either).walrusNotAllowedInComprehensionkey tolocalize.tsandpackage.nls.en-us.json.Error detection (which cases are flagged, and the diagnostic range) is unchanged — only the message text differs for the comprehension-iterable case.
Tests
assignmentExprMessage1.pyand testAssignmentExprMessage1assert the exact message for both branches: the comprehension-iterable case gets the new message, and a bare walrus in a comprehensionifkeeps the generic message.AssignmentExpr4test (which already exercises iterable-walrus cases) continues to pass with its error count unchanged, confirming detection is unaffected.Validation
Run in
packages/pyright-internal:npx tsc --noEmit— passes.npx prettier@2.8.8 -con all changed TS/JSON files — "All matched files use Prettier code style!".git diff --check— clean.npx jest parser tokenizer typeEvaluator1— all pass (includes the newAssignmentExprMessage1and the unchangedAssignmentExpr4).npx jest— all suites pass except LSP/type-server integration suites, which fail to run in this environment because they require the full monorepo bootstrap and the webpack test-server bundle (Cannot find module 'fs-extra'/Server bundle does not exist); these are unrelated to this change.Compatibility
No API changes. The only behavioral difference is a more accurate diagnostic message for walrus operators inside a comprehension iterable expression; the set of reported errors and their source ranges are unchanged.