fix(python): emit wildcard default for union or-patterns (#4649)#4653
Merged
Conversation
When an or-pattern over union cases shares its target with the default branch (e.g. `BestCase | MidCase -> true | WorstCase -> false`), the Python match generator dropped the wildcard `case _:` because the default's target index was already used by an explicit case. The union tag that was only reachable via the default then matched nothing and the function returned None. Always emit the wildcard default and instead drop the redundant explicit cases that route to the default's target, since the wildcard covers them. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Python Type Checking Results (Pyright)
Excluded files with errors (4 files)These files have known type errors and are excluded from CI. Remove from
|
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.
Fixes #4649
Problem
Matching against multiple union cases in an or-pattern returned
None(Python) for one of the cases:transpiled to (tag 0 missing):
so
atLeastMid BestCasefell through the match and returnedNoneinstead ofTrue.Root cause
In
transformSwitchPatternAsMatch, FCS lowered the match soBestCase(tag 0) became the default branch, sharing its target body (return True) withMidCase. The generator had adefaultAlreadyCoveredcheck that dropped the wildcardcase _:whenever the default's target index was already used by an explicit case — leaving the tag only reachable via the default (tag 0) unmatched.Fix
Always emit the wildcard default (it's the only thing covering subject values not listed explicitly). Instead, drop the redundant explicit cases that route to the default's target, since the wildcard already covers them. The example now generates:
Testing
test or-pattern over union cases sharing default targetintests/Python/TestPatternMatch.fs.🤖 Generated with Claude Code