Search code examples
pythonpython-typingmypy

How to type hint a Callable of a function with default arguments?


I'm trying to Type Hint the function bar, but I got the Too few arguments error when I run mypy.

from typing import Callable, Optional

def foo(arg: int = 123) -> float:
    return arg+0.1

def bar(foo: Callable[[int], float], arg: Optional[int] = None) -> float:
    if arg:
        return foo(arg)
    return foo()

print(bar(foo))
print(bar(foo, 90))

I have also tried:

  • Callable[[], float] (got Too many arguments error)
  • Callable[[Optional[int]], float] (got another error)

So, how should I do the Type Hinting of the bar function?


Solution

  • Define this:

    class Foo(Protocol):
        def __call__(self, x: int = ..., /) -> float:
            ...
    

    then type hint foo as Foo instead of Callable[[int], float]. Callback protocols allow you to:

    define flexible callback types that are hard (or even impossible) to express using the Callable[...] syntax

    and optional arguments are one of those impossible things to express with a normal Callable. The / at the end of __call__'s signature makes x a positional-only parameter, which allows any passed function to bar to have a parameter name that is not x (your specific example of foo calls it arg instead). If you removed /, then not only would the types have to line up as expected, but the names would have to line up too because you would be implying that Foo could be called with a keyword argument. Because bar doesn't call foo with keyword arguments, opting into that behavior by omitting the / imposes inflexibility on the user of bar (and would make your current example still fail because "arg" != "x").