News for hackers. Stuff that matters.
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)
Will give you `Too few arguments` for `fun(2)`

You can fix this with something like:

  class FunC(Protocol):
      def __call__(self, b: int, c: Optional[int] = None) -> str: ...

  def make_fun(a: int) -> FunC:  ...
But that that seems like unnecessary overhead, because the non-nested def case can happily understand the optional parameter.

There are no comments on this comment yet.