Fix union fallback skipped for assignments with ternary expressions (#21273)#21293
Open
armorbreak001 wants to merge 1 commit intopython:masterfrom
Open
Fix union fallback skipped for assignments with ternary expressions (#21273)#21293armorbreak001 wants to merge 1 commit intopython:masterfrom
armorbreak001 wants to merge 1 commit intopython:masterfrom
Conversation
The union_fallback path in infer_rvalue_with_fallback_context() was being incorrectly skipped when the rvalue contained a ternary (ConditionalExpr), because ternary expressions increment the binder version just like walrus operators do. This caused valid type narrowing via reassignment to be lost. Replace the binder_version check with an explicit check for AssignmentExpr (walrus) in the expression tree, which is more precise and doesn't falsely trigger on ternary expressions. Fixes python#21273
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
The
union_fallbackpath ininfer_rvalue_with_fallback_context()was being incorrectly skipped when the rvalue contained a ternary (ConditionalExpr), because ternary expressions increment the binder version (viaframe_context(can_skip=True, fall_through=0)invisit_conditional_expr) just like walrus operators do.This caused valid type narrowing via reassignment to be lost. For example:
Fix
Replace the
binder_version == self.binder.versioncheck with an explicit check forAssignmentExpr(walrus) in the expression tree. This is more precise and does not falsely trigger on ternary expressions or other constructs that happen to increment the binder version.The helper method
_expr_has_assignment_expr()walks the expression tree to check for the presence of anyAssignmentExprnode.Fixes #21273