Fix type evaluation of generator expressions inside contextually typed list/set literals (#11841) by patrickswedish · Pull Request #11889 · microsoft/pyright · GitHub
Skip to content

Fix type evaluation of generator expressions inside contextually typed list/set literals (#11841) - #11889

Open
Patrick Ribbsaeter (patrickswedish) wants to merge 1 commit into
microsoft:mainfrom
patrickswedish:fix/list-set-generator-comprehension-11841
Open

Patrick Ribbsaeter (patrickswedish) wants to merge 1 commit into
microsoft:mainfrom
patrickswedish:fix/list-set-generator-comprehension-11841

Conversation

@patrickswedish

@patrickswedish Patrick Ribbsaeter (patrickswedish) commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #11841.

In getTypeOfListOrSetWithContext, ParseNodeType.Comprehension entries were dispatched to getTypeOfComprehension(entry, elementContext) without checking !entry.d.isGenerator (unlike getTypeOfListOrSetInferred, which already checks entry.nodeType === ParseNodeType.Comprehension && !entry.d.isGenerator).

When a generator expression such as (i for i in range(4)) appeared as an element inside a contextually typed list or set literal (e.g. v: list[int] = [1, 2, (i for i in range(4))]), it was evaluated as a list comprehension against elementContext (yielding int) instead of being evaluated as a Generator expression via getTypeOfExpression(entry, elementContext).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to correctly identify the generator

1 participant