Skip to content

generic with constraints fails to narrow when using conditional expression #13048

Closed as not planned
@DetachHead

Description

@DetachHead
from typing import TypeVar

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

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

def foo(t: T):
    t.b if isinstance(t, B) else t.a # error: "B" has no attribute "a"
    
    if isinstance(t, B):
        t.b # no error
    else:
        t.a

https://mypy-play.net/?mypy=0.961&python=3.10&gist=1646ac27c8249ac4752a973d9b2b54d4

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