91

I heavily use python typing support from python 3.

Recently I was trying to pass a function as an argument and I do not find any help for using kwargs in typing.Callable signature.

Please check the code below and the comments.

import typing

# some function with a type signature
def fn1_as_arg_with_kwargs(a: int, b: float) -> float:
    return a + b

# some function with a type signature
def fn2_as_arg_with_kwargs(a: int, b: float) -> float:
    return a * b

# function that get callables as arg
# this works with typing
def function_executor(
        a: int, 
        b: float, 
        fn: typing.Callable[[int, float], float]):
    return fn(a, b)

# But what if I want to name my kwargs 
# (something like below which does not work)
# ... this will help me more complex scenarios 
# ... or am I expecting a lot from python3 ;)
def function_executor(
        a: int, 
        b: float, 
        fn: typing.Callable[["a": int, "b": float], float]):
    return fn(a=a, b=b)
3
  • When you define a function you specify its signature, now including the type of the arguments. There is little value in specifying the structure of a dict as an argument, especially **kwargs, which simply collects the named args, i.e. it depends on how the function is called. Use other appropriate and specific types instead (e.g. NamedTuple, Dataclass, Enum, custom classes), that is to say explicitate each argument as usual
    – Pynchia
    Commented Sep 7, 2019 at 22:06
  • I do use NamedTuple in some cases as an argument, which is a savior in some scenarios. But was curious if the above thing is supported as I need to typedef some legacy functions. If it is not at all supported please let me know as that might be more helpful. Commented Sep 7, 2019 at 22:32
  • github.com/python/typing/issues/239 this one?
    – shawn
    Commented Sep 8, 2019 at 9:10

2 Answers 2

128

You are probably looking for Callback protocols.

In short, when you want to express a callable with a complex signature, what you'll want to do is to create a custom Protocol that defines a __call__ method with the precise signature you want.

For example, in your case:

from typing import Protocol

# Or, if you want to support Python 3.7 and below, install the typing_extensions
# module via pip and do the below:
from typing_extensions import Protocol

class MyCallable(Protocol):
    def __call__(self, a: int, b: float) -> float: ...

def good(a: int, b: float) -> float: ...

def bad(x: int, y: float) -> float: ...


def function_executor(a: int, b: float, fn: MyCallable) -> float:
    return fn(a=a, b=b)

function_executor(1, 2.3, good)  # Ok!
function_executor(1, 2.3, bad)   # Errors

If you try type-checking this program using mypy, you'll get the following (admittedly cryptic) error on the last line:

Argument 3 to "function_executor" has incompatible type "Callable[[int, float], float]"; expected "MyCallable"

(Callback protocols are somewhat new, so hopefully the quality of the error messages will improve over time.)

1
  • 2
    What version of mypy were you using? This example doesn't seem to throw error messages for the 'bad' case with mypy 1.2.0 or 1.1.1
    – laker93
    Commented Apr 12, 2023 at 12:55
16

I found the example with the typing callback a bit complicated. For anyone looking for a simple example of typing a function with kwargs:

from typing import Protocol

class MyCallable(Protocol):
    # Define types here, as if __call__ were a function (ignore self).
    def __call__(self, a: int, b: int) -> int:
        ...

# Generic function- types correspond to MyCallable.__call__ args.
def func_add(a: int, b: int) -> int:
    return a + b

# Assign the function to a variable called my_function, and add the type.
my_function: MyCallable = func_add

my_function(a=1, b=2)   # This is OK.
my_function(a=1, b="x") # This is NOK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.