Skip to content

[match-case] class pattern narrowing with Union-argument different from equivalent isinstance call. #19468

@randolf-scholz

Description

@randolf-scholz
class Foo: ...
class Bar(Foo): ...

def test_1(bar: Bar) -> None:
    match bar:
        case Foo() as foo:
            reveal_type(foo)  # N: Revealed type is "__main__.Bar"
            
def test_2(bar: Bar | str) -> None:
    match bar:
        case Foo() as foo:
            reveal_type(foo)  # N: Revealed type is "__main__.Foo"
            
def test_3(bar: Bar) -> None:
    if isinstance(bar, Foo):
        reveal_type(bar)  # N: Revealed type is "__main__.Bar"
            
def test_4(bar: Bar | str) -> None:
    if isinstance(bar, Foo):
        reveal_type(bar)  # N: Revealed type is "__main__.Bar"

https://mypy-play.net/?mypy=latest&python=3.12&gist=4bc28e67967c7809ccdd0962f3372dd7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statementtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions