Skip to content
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

Fix narrowing on match with function subject #16503

Merged
merged 5 commits into from
Feb 17, 2024
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
19 changes: 16 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5043,6 +5043,19 @@ def visit_continue_stmt(self, s: ContinueStmt) -> None:
return None

def visit_match_stmt(self, s: MatchStmt) -> None:
named_subject: Expression
if isinstance(s.subject, CallExpr):
# Create a dummy subject expression to handle cases where a match statement's subject
# is not a literal value. This lets us correctly narrow types and check exhaustivity
# This is hack!
id = s.subject.callee.fullname if isinstance(s.subject.callee, RefExpr) else ""
name = "dummy-match-" + id
v = Var(name)
named_subject = NameExpr(name)
named_subject.node = v
else:
named_subject = s.subject

with self.binder.frame_context(can_skip=False, fall_through=0):
subject_type = get_proper_type(self.expr_checker.accept(s.subject))

Expand All @@ -5061,7 +5074,7 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
# The second pass narrows down the types and type checks bodies.
for p, g, b in zip(s.patterns, s.guards, s.bodies):
current_subject_type = self.expr_checker.narrow_type_from_binder(
s.subject, subject_type
named_subject, subject_type
)
pattern_type = self.pattern_checker.accept(p, current_subject_type)
with self.binder.frame_context(can_skip=True, fall_through=2):
Expand All @@ -5072,7 +5085,7 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
else_map: TypeMap = {}
else:
pattern_map, else_map = conditional_types_to_typemaps(
s.subject, pattern_type.type, pattern_type.rest_type
named_subject, pattern_type.type, pattern_type.rest_type
)
self.remove_capture_conflicts(pattern_type.captures, inferred_types)
self.push_type_map(pattern_map)
Expand Down Expand Up @@ -5100,7 +5113,7 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
and expr.fullname == case_target.fullname
):
continue
type_map[s.subject] = type_map[expr]
type_map[named_subject] = type_map[expr]

self.push_type_map(guard_map)
self.accept(b)
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,21 @@ match m:

reveal_type(a) # N: Revealed type is "builtins.str"

[case testMatchCapturePatternFromFunctionReturningUnion]
def func1(arg: bool) -> str | int: ...
def func2(arg: bool) -> bytes | int: ...

def main() -> None:
match func1(True):
case str(a):
match func2(True):
case c:
reveal_type(a) # N: Revealed type is "builtins.str"
reveal_type(c) # N: Revealed type is "Union[builtins.bytes, builtins.int]"
reveal_type(a) # N: Revealed type is "builtins.str"
case a:
reveal_type(a) # N: Revealed type is "builtins.int"

-- Guards --

[case testMatchSimplePatternGuard]
Expand Down
Loading