how in python create a type based on return type of a function

28 Views Asked by At

I'm using a library that doesn't have any stub

so I created a stub for it but now I have issue to use those types that I've created for the stub since it's not part of the original library I can't import them

here is an example

here is the stub:

class IdInfoTuple(NamedTuple):
    custom: bool
    chart_mode: int
    select: bool
    visible: bool


def id_info(id: str, /) -> Optional[IdInfoTuple]:

now in my code I need to pass around the IdInfoTuple but I can't

for example when I create a function like this

def some_func(idInfo:<how to tell this is type of IdInfo?>):
   pass

I thought if I can do something like typescript where we can create a type based on return type of a function like this

type IdInfoTuple= ReturnType<typeof id_info>;

but I can't find how to do it in python. is it possible? if yes how to achieve it? something that help pylance find the correct type. I'm not looking to find type at execution time, but at the coding time help with strongly type my function

0

There are 0 best solutions below