Skip to content

[match-case]: mapping pattern narrowing inconsistent with equivalent isinstance narrowing #11289

@randolf-scholz

Description

@randolf-scholz

Code sample in pyright playground

from typing import assert_type
from collections.abc import Mapping

def test_instance(x: dict | int) -> None:
    if isinstance(x, Mapping):
        assert_type(x, dict)  # ✅️
    else:
        assert_type(x, int)  # ✅️

def test_match(x: dict | int) -> None:
    match x:
        case {}:  # equivalent to isinstance(x, Mapping)
            assert_type(x, dict)  # ❌️
        case _:
            assert_type(x, int)  # ❌️

Additionally, there are some failures related to TypeDict with mapping patterns: Code sample in pyright playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions