You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't have an alias that just targets a type variable #543
T=TypeVar('T', int, float)
MyType=Union[T, List[T]]
# in the user's codeU=TypeVar('U', int, float)
defmyfunc(MyType[U], U): ...
In version 2, I decide to drop support for passing List[T]. However, this causes an error on the downstream type definitions
T=TypeVar('T', int, float)
MyType=Union[T] # or Generic[T], or T - all fail# downstream code doesn't change - I want the freedom to change `MyType` again in futureU=TypeVar('U', int, float)
defmyfunc(MyType[U], U): ... # fails due to missing __getitem__
Is there a way to write this? Does implementing TypeVar.__getitem__ seem reasonable? Or should we introduce Identity[T], which boxes a typevar into a generic?
Activity
ilevkivskyi commentedon Mar 14, 2018
This is the first time I see such request. TBH, I think it could be rather confusing for someone reading the code like this:
So I am strong -1 on this.
eric-wieser commentedon Mar 14, 2018
So one related problem at the moment is the result of
Uniondepends on it's arguments. This succeedsYet this fails
Contrast this with the equivalent C++ code, which just works:
eric-wieser commentedon Mar 14, 2018
Note that I give a real application in the referenced issue above. In python 3, we have
But there is no way to specify this type on python 2, where
PathLikedoesn't exist, and the definition is justilevkivskyi commentedon Jun 20, 2019
Hm, it looks like an experimental
mypy_extensions.FlexibleAliasis exactly what can help in this case.[-]TypeVars should support indexing[/-][+]Can't have an alias that just targets a type variable[/+]JelleZijlstra commentedon May 22, 2023
PEP 695 addresses this. (Not on Python 2, but we don't care about that any more.)