Skip to content

isinstance fails to narrow generic value bound to a union on the else branchΒ #13049

@DetachHead

Description

@DetachHead
from typing import TypeVar

class A:
    a: int
class B:
    b: int

T = TypeVar("T", bound=A | B)

def foo(t: T):
    if isinstance(t, B):
        t.b
    else:
        t.a #  error: Item "B" of the upper bound "Union[A, B]" of type variable "T" has no attribute "a"

https://mypy-play.net/?mypy=0.961&python=3.10&gist=024a91f8de4727ab17e124696dc58994

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions