Fix 1.20 regression: type narrowing lost on reassignment with generator/ternary expressions#21280
Draft
parinporecha wants to merge 3 commits intopython:masterfrom
Draft
Conversation
…ion guard The binder version guard introduced in PR python#20622 was too broad: it disabled union fallback for any expression that modifies the binder, including generator expressions with ternary operators. This caused type narrowing via reassignment to fail for non-walrus expressions. Replace the binder_version == self.binder.version check with an explicit check for AssignmentExpr (walrus :=) in the expression tree, which is the actual problematic case. This restores type narrowing for generator expressions and other non-walrus constructs while preserving the walrus safety guard.
This comment has been minimized.
This comment has been minimized.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: graphql-core (https://github.com/graphql-python/graphql-core)
+ src/graphql/execution/execute.py:1793: error: Unused "type: ignore" comment [unused-ignore]
|
ilevkivskyi
requested changes
Apr 21, 2026
Member
ilevkivskyi
left a comment
There was a problem hiding this comment.
This will have horrible performance implications. Since the decision is made post-visit, a simple flag set in visit_assignment_expression() would be sufficient. Moreover, that flag can be set only if the binder was actually used.
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
PR #20622 introduced a
binder_versionguard to prevent false negatives when walrus operators (:=) interact with type inference fallback logic. However, this guard was too broad: any expression that bumps the binder version (including generator expressions with ternary operators) would disable the union fallback path, causing type narrowing to be lost on reassignment.Problem
This worked in mypy 1.19 but regressed in 1.20.
Fix
Replace the
binder_version == self.binder.versioncheck with an explicit check forAssignmentExpr(walrus:=) in the expression tree, following the suggestion from @ilevkivskyi in the issue.The walrus operator is the specific construct that causes problematic binder mutations during r.h.s. acceptance. Other constructs that bump the binder version (ternary in generators, etc.) are benign and should not disable the fallback.
Changes
_AssignmentExprSeeker(TraverserVisitor) to detect walrus in expression treesTypeChecker._has_assignment_expr()static methodbinder_version == self.binder.versionwithnot has_walrusinunion_fallbackguardTesting
testAssignToOptionalTupleWalrusandtestReturnTupleOptionalWalrusIssue 21273