Skip to content

Type narrowing does not work when a tuple with union and other type is passed to isinstance #1831

Description

@PerchunPak

Description

isinstance can accept union types as an argument, but when I use a union and another type in the isinstance call, basedpyright fails to narrow types

from typing import reveal_type, TypeAlias

class A: ...
class B: ...
class C: ...

Alias: TypeAlias = A | B

def get_foo() -> Alias | None: ...

foo: Alias | None = get_foo()

if isinstance(foo, Alias):
    # this works, see https://github.com/microsoft/pyright/issues/2565
    reveal_type(foo) # A | B
if isinstance(foo, (Alias, C)):
    # expected A | B | C
    # but got  A | B | None
    reveal_type(foo)

Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    type checking / lintingissues relating to existing diagnostic rules or proposals for new diagnostic rulesupstream buga bug that also exists in pyright but has not been raised upstream

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions