Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Better match narrowing for irrefutable mapping patterns
  • Loading branch information
hauntsaninja committed Feb 26, 2026
commit 6e84a6727d88250ec214b192736e451031c2d782
12 changes: 11 additions & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,22 @@ def visit_mapping_pattern(self, o: MappingPattern) -> PatternType:

captures[o.rest] = rest_type

else_type = current_type
if can_match:
# We can't narrow the type here, as Mapping key is invariant.
new_type = self.type_context[-1]
if not o.keys:
# Match cannot be refuted, so narrow the remaining type
mapping = self.chk.named_type("typing.Mapping")
new_type, else_type = self.chk.conditional_types_with_intersection(
current_type,
[TypeRange(mapping, is_upper_bound=False)],
o,
default=current_type,
)
else:
new_type = UninhabitedType()
return PatternType(new_type, current_type, captures)
return PatternType(new_type, else_type, captures)

def get_mapping_item_type(
self, pattern: MappingPattern, mapping_type: Type, key: Expression
Expand Down
40 changes: 39 additions & 1 deletion test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,45 @@ match m:
reveal_type(r) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
[builtins fixtures/dict.pyi]

-- Mapping patterns currently do not narrow --
[case testMatchMappingPatternNarrowing]
from typing import Mapping, Sequence

def f1(x: dict[str, str] | list[str] | str) -> None:
match x:
case {}:
reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str, builtins.str]"
case [*_]:
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]"
case _:
reveal_type(x) # N: Revealed type is "builtins.str"

def f1_rest(x: dict[str, str] | list[str] | str) -> None:
match x:
case {**rest}:
reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str, builtins.str]"
case [*_]:
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]"
case _:
reveal_type(x) # N: Revealed type is "builtins.str"

def f2(x: Mapping[str, str] | Sequence[str] | str) -> None:
match x:
case {}:
reveal_type(x) # N: Revealed type is "typing.Mapping[builtins.str, builtins.str]"
case [*_]:
reveal_type(x) # N: Revealed type is "typing.Sequence[builtins.str]"
case _:
reveal_type(x) # N: Revealed type is "builtins.str"

def f2_rest(x: Mapping[str, str] | Sequence[str] | str) -> None:
match x:
case {**rest}:
reveal_type(x) # N: Revealed type is "typing.Mapping[builtins.str, builtins.str]"
case [*_]:
reveal_type(x) # N: Revealed type is "typing.Sequence[builtins.str]"
case _:
reveal_type(x) # N: Revealed type is "builtins.str"
[builtins fixtures/dict.pyi]

-- Class Pattern --

Expand Down