Skip to content

Use union for captured vars in or pattern #19710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def visit_or_pattern(self, o: OrPattern) -> PatternType:
for capture_list in capture_types.values():
typ = UninhabitedType()
for _, other in capture_list:
typ = join_types(typ, other)
typ = make_simplified_union([typ, other])

captures[capture_list[0][0]] = typ

Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1204,13 +1204,13 @@ match m:
case 1 | "foo":
reveal_type(m) # N: Revealed type is "Union[Literal[1], Literal['foo']]"

[case testMatchOrPatterCapturesMissing]
[case testMatchOrPatternCapturesMissing]
from typing import List
m: List[int]

match m:
case [x, y] | list(x): # E: Alternative patterns bind different names
reveal_type(x) # N: Revealed type is "builtins.object"
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]"
reveal_type(y) # N: Revealed type is "builtins.int"
[builtins fixtures/list.pyi]

Expand All @@ -1219,7 +1219,7 @@ m: object

match m:
case list(x) | dict(x):
reveal_type(x) # N: Revealed type is "typing.Iterable[Any]"
reveal_type(x) # N: Revealed type is "Union[builtins.list[Any], builtins.dict[Any, Any]]"
[builtins fixtures/dict.pyi]

-- Interactions --
Expand Down Expand Up @@ -1405,7 +1405,7 @@ m: Union[str, bytes, int]

match m:
case str(a) | bytes(a):
reveal_type(a) # N: Revealed type is "builtins.object"
reveal_type(a) # N: Revealed type is "Union[builtins.str, builtins.bytes]"
reveal_type(m) # N: Revealed type is "Union[builtins.str, builtins.bytes]"
case b:
reveal_type(b) # N: Revealed type is "builtins.int"
Expand Down
Loading