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
Prev Previous commit
Special case Any
  • Loading branch information
hauntsaninja committed Feb 26, 2026
commit b3706b43381db2347f6e06e11eed558e750152cd
4 changes: 3 additions & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,14 @@ def visit_mapping_pattern(self, o: MappingPattern) -> PatternType:
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(
if_type, else_type = self.chk.conditional_types_with_intersection(
current_type,
[TypeRange(mapping, is_upper_bound=False)],
o,
default=current_type,
)
if not isinstance(current_type, AnyType):
new_type = if_type
else:
new_type = UninhabitedType()
return PatternType(new_type, else_type, captures)
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,20 @@ def f2_rest(x: Mapping[str, str] | Sequence[str] | str) -> None:
reveal_type(x) # N: Revealed type is "builtins.str"
[builtins fixtures/dict.pyi]

[case testMatchMappingPatternNarrowingAny]
from typing import Any

def f1(x: Any) -> None:
match x:
case {}:
reveal_type(x) # N: Revealed type is "Any"

def f2(x: Any) -> None:
match x:
case {"x": "y"}:
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/dict.pyi]

-- Class Pattern --

[case testMatchClassPatternCapturePositional]
Expand Down