case Cls() and isinstance(obj, Cls) should give the same narrowing behavior, but in the example below, the results differ when obj is a union-type.
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
case Cls()andisinstance(obj, Cls)should give the same narrowing behavior, but in the example below, the results differ whenobjis a union-type.https://mypy-play.net/?mypy=latest&python=3.12&gist=4bc28e67967c7809ccdd0962f3372dd7