Levan Gharibashvili
Levan Gharibashvili

Reputation: 73

Typing for a Python function that takes a generic class and returns a specific instance of that class

Suppose I have a class definition like this:

T = TypeVar('T')

class Collection(Generic[T]):
    ...

I want to create a function like this (Data is some container type):

T = TypeVar('T')

def get_data(typ: Type[T]) -> T[Data]:
    return typ[Data](...)

Basically, I would like this function to accept a generic type with one type variable and return instance of that type but with type variable set to some other type

Upvotes: 1

Views: 1085

Answers (0)

Related Questions