(🎁) Support type variables in the bounds of other type variables
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
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 TypeAliases:
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
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the TypeVar and generic TypeAlias examples in the issue, then compare the current handling of bounds and inferred types. The work is done when the shown reveal_type outputs treat T as bound through L and produce the expected union for foo(a).
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100