[ty] Fix edge-case bugs when narrowing tagged unions in match statements#22870
Merged
AlexWaygood merged 3 commits intomainfrom Jan 30, 2026
Merged
[ty] Fix edge-case bugs when narrowing tagged unions in match statements#22870AlexWaygood merged 3 commits intomainfrom
match statements#22870AlexWaygood merged 3 commits intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅ |
|
This comment was marked as resolved.
This comment was marked as resolved.
AlexWaygood
pushed a commit
that referenced
this pull request
Jan 30, 2026
…b-patterns This test demonstrates a bug in match statement narrowing where an OR pattern containing a non-literal value type (like a class attribute with type `str`) incorrectly narrows the TypedDict union. The bug causes: - `case Config.mode | "foo":` to narrow to `Foo` instead of `Foo | Bar` - Subsequent `case "bar":` to see `Never` instead of `Bar` This test should fail on main but pass with PR #22870, which changes `evaluate_match_pattern_value` to use `.unwrap_or_default()` instead of `?` when `evaluate_expr_compare_op` returns `None`. https://claude.ai/code/session_01NAVrriYXEB5X7KJcmNyeWn
a9fec0e to
8a5642e
Compare
AlexWaygood
pushed a commit
that referenced
this pull request
Jan 30, 2026
When an OR pattern in a match statement contains a sub-pattern with a non-single-valued type (like `Config.mode: str`), we cannot definitively exclude any specific type from subsequent patterns. This is because the non-literal pattern could match any value of its type at runtime. For example, in `case Config.mode | "foo":`, if `Config.mode` has type `str`, the pattern could match any string value. We cannot narrow based on `"foo"` alone because the actual match might have been against `Config.mode`. This fix makes two changes: 1. In `pattern_kind_to_type`, for `Value` patterns, only return the type if it's single-valued. For non-single-valued types, return `Never` to indicate we can't exclude any specific type. 2. For `Or` patterns, if any sub-pattern returns `Never` (can't exclude anything), return `Never` for the whole OR pattern. Also includes the PR #22870 fix to use `.unwrap_or_default()` instead of `?` in `evaluate_match_pattern_value`, which allows TypedDict/tuple narrowing to continue even when the compare op returns `None`. https://claude.ai/code/session_01NAVrriYXEB5X7KJcmNyeWn
match statements
Member
Author
|
This PR had an interesting development process:
|
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
Fix two edge-case bugs in our narrowing/reachability logic that could cause issues when narrowing tagged unions of TypedDicts or tuples:
An incorrect early return in
narrow.rsmeant that we were apply incorrect positive-case narrowing to tagged unions forORpatterns where one element in the pattern had aLiteraltype and another did not:pattern_kind_to_typeinreachability_constraints.rswas incorrectly returning the inferred type of allVALUEpatterns. But that's only correct if theVALUEpattern has a single-valued type[^1]. If theVALUEpattern does not have a single-valued type, the set of values that would definitely lead to thatmatchbranch being taken is the empty set -- so returningNeverhere is more appropriate. This bug led to incorrect inferences ofNeverin subsequent branches ofmatchstatements involving TypedDicts and tuples:Test plan
I've added new mdtests.
It's sort-of interesting that this PR has no primer impact at all -- I guess that just shows that pattern-matching still is pretty rarely used across the Python ecosystem. It takes a while for new syntax features to gain widespread adoption!