Re:Python's Type Checking Renaissance
by jacobr1 on Wednesday January 27, 2021 @10:58AM
(#25929812)
More annoying for our codebase is the inability to specify optional/default arguments with closures. We often use functions that return other functions and you can't persist the "optional state" without using a protocol. It would be great just to match the current state of vanilla defs with optional params.
For example:
def make_fun1(a: int) -> Callable[[int, Optional[int]], str]:
def fun(b: int, c: Optional[int] = None) -> str:
if c:
b += c
return f"{a+b}"
return fun
fun = make_fun(1)
fun(2)
For example:
Will give you `Too few arguments` for `fun(2)`You can fix this with something like:
But that that seems like unnecessary overhead, because the non-nested def case can happily understand the optional parameter.