#4621·ty

False negative `unresolved-attribute` on a narrowed TypeVar with a union upper bound

Author: ste7enCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbugnarrowinggenericsset-theoretic typesattributes

Summary

After narrowing a TypeVar whose upper bound is a union, ty stops reporting attribute accesses that are only valid on some members of the bound. The same code using the union directly (no TypeVar) is correctly reported, as is the un-narrowed TypeVar.

python
from typing import reveal_type


class A:
    y: int


class B: ...


def with_union(v: A | B) -> int:
    if isinstance(v, A):
        return 0
    return v.y  # error: Object of type `B & ~A` has no attribute `y`  ✅


def with_typevar[T: A | B](v: T) -> int:
    if isinstance(v, A):
        return 0
    reveal_type(v)  # T@with_typevar & ~A
    return v.y  # no error ❌


def positive_narrowing[T: A | B](v: T) -> int:
    if isinstance(v, B):
        reveal_type(v)  # T@positive_narrowing & B
        return v.y  # no error ❌
    return 0

Accessing an attribute that exists on no member of the bound (e.g. v.does_not_exist) is still reported, so it looks like attribute lookup on T & X doesn't distribute the union bound of T (e.g. (A | B) & ~A → B & ~A) and treats the attribute as available because it exists on one bound member. The same happens with legacy TypeVar("T", bound=A | B) and with match narrowing (case A(): ... case _: v.y). mypy reports all of these.

Command: ty check repro.py Repro: https://play.ty.dev/6be32611-958b-4319-9fca-5267f7ece38a

Version

ty 0.0.84 (8dd9a7f7f 2026-09-24)