How do I specify the type of a callback function which takes an optional parameter?
For example, I have the following function:
def getParam(param: str = 'default'):
return param
I'd like to pass it on as a callback function and use it both as Callable[[str], str]
and Callable[[], str]
:
def call(callback: Callable[[<???>], str]):
return callback() + callback('a')
What do I write for <???>
? I've tried Python's Optional
but that does not work, as it still enforces a parameter (even if with value None
) to be present.
For reference, I'm basically searching for the equivalent of the following TypeScript:
function call(callback: (a?: string) => string) {
return callback() + callback('a');
}