Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express python callable type without determining return type?

I have a function which returns another function, but the return type of returned function is not clear. To express function return type we can use Callable[[], ReturnType], and to not emphasize on arguments we can do this Callable[..., ReturnType]. But I want this ReturnType to not be determined.

What should I do?

My code is something like this:

def funcA() -> int:
  return 2

def funcB() -> str:
  return 'b'

def f(inp: int) -> Callable[[], X] # <--- what should X be Here?
  return funcA if inp == 0 else funcB
like image 635
Sajad Rezvani Avatar asked Apr 13 '26 21:04

Sajad Rezvani


1 Answers

In theory, f returns either int or str, so the return type should be typing.Union[int, str].

If you don't want to specify the return type then you can use typing.Any.

like image 89
HKG Avatar answered Apr 15 '26 09:04

HKG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!