-
Notifications
You must be signed in to change notification settings - Fork 258
Can't have an alias that just targets a type variable #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is the first time I see such request. TBH, I think it could be rather confusing for someone reading the code like this: T = TypeVar('T', int, float)
U = TypeVar('U', int, float)
MyType = T
def func(arg: MyType[U]) -> U:
... So I am strong -1 on this. |
So one related problem at the moment is the result of
Yet this fails
Contrast this with the equivalent C++ code, which just works: template<typename T>
using X = union<T>;
X<string> s; template<typename T>
using X = union<T, int>;
X<string> s; |
Note that I give a real application in the referenced issue above. In python 3, we have T = TypeVar('T', *AnyStr.__constraints__)
PathType = Union[T, PathLike[T]]
# use PathType[str], PathType[bytes], PathType[AnyStr], etc But there is no way to specify this type on python 2, where T = TypeVar('T', *AnyStr.__constraints__)
PathType = ???[T]
# PathType needs to be indexable just like in the py3 case |
Hm, it looks like an experimental |
PEP 695 addresses this. (Not on Python 2, but we don't care about that any more.) type MyType[T: (int, float)] = T
# Or
from typing_extensions import TypeAliasType, TypeVar
T = TypeVar("T", int, float)
MyType = TypeAliasType("MyType", T, type_params=(T,))
def myfunc[U: (int, float)](a: MyType[U], b: U): ... |
Consider version 1 of my package, which defines
In version 2, I decide to drop support for passing
List[T]
. However, this causes an error on the downstream type definitionsIs there a way to write this? Does implementing
TypeVar.__getitem__
seem reasonable? Or should we introduceIdentity[T]
, which boxes a typevar into a generic?The text was updated successfully, but these errors were encountered: