Код: Выделить всё
def f(*classes):
return tuple(cls() for cls in classes)
< /code>
Есть ли способ аннотировать его, чтобы Mypy (и, что более важно, Intellisense) понимал это отношение? Например: < /p>
a, b, c = f(A, B, C)
# reveal_type(a) is A
# reveal_type(b) is B
# reveal_type(c) is C
Код: Выделить всё
def f[*Ts](
*classes: type[*Ts] # this; as in, tuple[type[T] for T in Ts]
) -> tuple[*Ts]:
return tuple(cls() for cls in classes)
Код: Выделить всё
Unpack is only valid in a variadic positionedit < Br /> Пока что единственное решение, которое я мог бы придумать,-это что-то вроде: < /p>
Код: Выделить всё
@overload
def f[T1](cls1: type[T1]) -> tuple[T1]: ...
@overload
def f[T1, T2](cls1: type[T1], cls2: type[T2]) -> tuple[T1, T2]: ...
@overload
def f[T1, T2, T3](cls1: type[T1], cls2: type[T2], cls3: type[T3]) -> tuple[T1, T2, T3]: ...
# 10 suchoverloads, assuming nobody is going to call the function with 11 classes or more...
Подробнее здесь: https://stackoverflow.com/questions/793 ... pevartuple
Мобильная версия