@@ -620,7 +620,59 @@ match m:
620620 reveal_type(r) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
621621[builtins fixtures/dict.pyi]
622622
623- -- Mapping patterns currently do not narrow --
623+ [case testMatchMappingPatternNarrowing]
624+ from typing import Mapping, Sequence
625+
626+ def f1(x: dict[str, str] | list[str] | str) -> None:
627+ match x:
628+ case {}:
629+ reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str, builtins.str]"
630+ case [*_]:
631+ reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]"
632+ case _:
633+ reveal_type(x) # N: Revealed type is "builtins.str"
634+
635+ def f1_rest(x: dict[str, str] | list[str] | str) -> None:
636+ match x:
637+ case {**rest}:
638+ reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str, builtins.str]"
639+ case [*_]:
640+ reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]"
641+ case _:
642+ reveal_type(x) # N: Revealed type is "builtins.str"
643+
644+ def f2(x: Mapping[str, str] | Sequence[str] | str) -> None:
645+ match x:
646+ case {}:
647+ reveal_type(x) # N: Revealed type is "typing.Mapping[builtins.str, builtins.str]"
648+ case [*_]:
649+ reveal_type(x) # N: Revealed type is "typing.Sequence[builtins.str]"
650+ case _:
651+ reveal_type(x) # N: Revealed type is "builtins.str"
652+
653+ def f2_rest(x: Mapping[str, str] | Sequence[str] | str) -> None:
654+ match x:
655+ case {**rest}:
656+ reveal_type(x) # N: Revealed type is "typing.Mapping[builtins.str, builtins.str]"
657+ case [*_]:
658+ reveal_type(x) # N: Revealed type is "typing.Sequence[builtins.str]"
659+ case _:
660+ reveal_type(x) # N: Revealed type is "builtins.str"
661+ [builtins fixtures/dict.pyi]
662+
663+ [case testMatchMappingPatternNarrowingAny]
664+ from typing import Any
665+
666+ def f1(x: Any) -> None:
667+ match x:
668+ case {}:
669+ reveal_type(x) # N: Revealed type is "Any"
670+
671+ def f2(x: Any) -> None:
672+ match x:
673+ case {"x": "y"}:
674+ reveal_type(x) # N: Revealed type is "Any"
675+ [builtins fixtures/dict.pyi]
624676
625677-- Class Pattern --
626678
0 commit comments