Skip to content

Commit 0f41d33

Browse files
committed
Narrow impossible values in mapping pattern to Never
1 parent 8fb6fab commit 0f41d33

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

mypy/checkpattern.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -469,14 +469,14 @@ def visit_mapping_pattern(self, o: MappingPattern) -> PatternType:
469469
captures: dict[Expression, Type] = {}
470470
for key, value in zip(o.keys, o.values):
471471
inner_type = self.get_mapping_item_type(o, current_type, key)
472-
if inner_type is None:
472+
if is_uninhabited(inner_type):
473473
can_match = False
474-
inner_type = self.chk.named_type("builtins.object")
474+
475475
pattern_type = self.accept(value, inner_type)
476476
if is_uninhabited(pattern_type.type):
477477
can_match = False
478-
else:
479-
self.update_type_map(captures, pattern_type.captures)
478+
479+
self.update_type_map(captures, pattern_type.captures)
480480

481481
if o.rest is not None:
482482
mapping = self.chk.named_type("typing.Mapping")
@@ -501,13 +501,13 @@ def visit_mapping_pattern(self, o: MappingPattern) -> PatternType:
501501

502502
def get_mapping_item_type(
503503
self, pattern: MappingPattern, mapping_type: Type, key: Expression
504-
) -> Type | None:
504+
) -> Type:
505505
mapping_type = get_proper_type(mapping_type)
506506
if isinstance(mapping_type, TypedDictType):
507507
with self.msg.filter_errors() as local_errors:
508-
result: Type | None = self.chk.expr_checker.visit_typeddict_index_expr(
509-
mapping_type, key
510-
)[0]
508+
result: Type = self.chk.expr_checker.visit_typeddict_index_expr(mapping_type, key)[
509+
0
510+
]
511511
has_local_errors = local_errors.has_new_errors()
512512
# If we can't determine the type statically fall back to treating it as a normal
513513
# mapping
@@ -516,7 +516,7 @@ def get_mapping_item_type(
516516
result = self.get_simple_mapping_item_type(pattern, mapping_type, key)
517517

518518
if local_errors.has_new_errors():
519-
result = None
519+
result = UninhabitedType()
520520
else:
521521
with self.msg.filter_errors():
522522
result = self.get_simple_mapping_item_type(pattern, mapping_type, key)

test-data/unit/check-python310.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,10 @@ m: A
544544
match m:
545545
case {1: v}:
546546
reveal_type(v) # E: Statement is unreachable \
547-
# N: Revealed type is "builtins.object"
547+
# N: Revealed type is "Never"
548548
case {b.b: v2}:
549549
reveal_type(v2) # E: Statement is unreachable \
550-
# N: Revealed type is "builtins.object"
550+
# N: Revealed type is "Never"
551551
[file b.py]
552552
b: int
553553
[typing fixtures/typing-typeddict.pyi]

0 commit comments

Comments
 (0)