| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
…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.
|
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]
|
Sorry, something went wrong.
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.
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Summary
PR #20622 introduced a binder_version guard 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.version check with an explicit check for AssignmentExpr (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
Testing
Issue 21273