Раскрытие typing.TypeVarTuple с "шаблонном"

Рейтинг: 0Ответов: 1Опубликовано: 18.06.2023

Как мне получить тип tuple[A[int], A[int]] для b.b как в коде для c++?

import typing

T = typing.TypeVar('T')
Ts = typing.TypeVarTuple('Ts')

class A(typing.Generic[T]):
    a: T

class B(typing.Generic[*Ts]):
    b: tuple[*A[Ts]] # <<< Ошибка Pylance: TypeVarTuple not allowed in this context

b: B[int, int] = B()
typing.reveal_type(b.b) # выводит не правильный тип: tuple[A[*tuple[int, int]]]
# Ожидалось tuple[A[int], A[int]]

Код c++

#include <iostream>
#include <tuple>
#include <typeinfo>

template<typename T>
struct A {
    T a;
};

template<typename... Ts>
struct B {
    std::tuple<A<Ts>...> b;
};

int main() {
    B<int, int> b;
    std::cout << typeid(b.b).name() << std::endl;
    // std::__1::tuple<A<int>, A<int>>
    return 0;
}

Я пробовал tuple[*(A[T1] for T1 in Ts)], но Pylance игнорирует это и ругается вообще на квадратную скобку.

Ответы

▲ 0

Я не силен в такой сложной типизации и не знаю cpp ) Но получилось сделать так, чтобы тип был tuple[A[int], A[int]] для b.b. Надеюсь будет полезно)

import typing

T = typing.TypeVar('T')
Ts = typing.TypeVarTuple('Ts')

class A(typing.Generic[T]):
    a: T

class B(typing.Generic[*Ts, T]):
    b: tuple[A[T], A[T]]

b: B[int, int] = B()
typing.reveal_type(b.b)