basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

Support `TypeVar`s in the bounds of other `TypeVar`s

Open KotlinIsland opened this issue 3 years ago • 1 comments

from typing import _T as T, TypeVar

L = TypeVar("L", bound=list[T])  # error: Type variable "typing._T" is unbound

def foo(l: L) -> L | T:  # error: A function returning TypeVar should receive at least one argument containing the same Typevar
    res = l[0]
    if res:
        return res
    return l

a: list[int] | list[str]

reveal_type(foo)  # "def [L <: list[T?], T] (l: L) -> L | T"
reveal_type(foo(a))  # list[int] | list[str]

KotlinIsland avatar Jul 23 '22 07:07 KotlinIsland

L = TypeVar("L", bound=list[T]) #type: ignore[valid-type]

this works but not when the bound is a TypeVar on its own

L = TypeVar("L", bound=T) #type:ignore[valid-type]

def foo(t: T, l: L) -> L:
    ...

a: int

reveal_type(foo(a, a)) # error

DetachHead avatar Jul 27 '22 04:07 DetachHead

@DetachHead is that #402?

KotlinIsland avatar Jan 07 '23 14:01 KotlinIsland