-
Notifications
You must be signed in to change notification settings - Fork 269
Open
Labels
topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations
Description
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]
Expected:
reveal_type(foo) # "def [T, L <: list[T]] (l: L) -> L | T"
reveal_type(foo(a)) # list[int] | list[str] | int | str
Mypy should understand that T
should be bound to foo
from L
This is identical to generic TypeAlias
es:
L: TypeAlias = list[T]
a: L[int]
Here T
is unbound, yet it's a valid and semantically sound expression.
Typescript example
The same idea could be represented in TypeScript as:
declare function foo<T, L extends T[]>(l: L): L | T
declare let a: number[] | string[]
let b = foo(a)
Although TS fails to infer the correct type here. (it infers as unknown
)
basedmypy
This is partially supported in basedmypy
DetachHead, smheidrich, aiudirog, ethanresnick, NoamNol and 2 more
Metadata
Metadata
Assignees
Labels
topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations