How to specify type for function parameter (Python)

Question:

I want to restrict scope of functions that can be passed as parameter to another function. For example, to restrict functions to be only one from two specified, or from particular module, or by signature. I tried the code below but in it there is now restrictions: as parameter can be passed any function.

Is this possible in Python?

def func_as_param():
    print("func_as_param called")


def other_func():
    print("other_func called")


def func_with_func_arg(func: func_as_param):  # this is not giving restrictions
# def func_with_func_arg(func: type(func_as_param)):  # this is also not giving restrictions
    print("func_with_func_arg called")
    func()


def test_func_with_func_arg():
    print("test_func_with_func_arg")
    func_with_func_arg(func_as_param)
    func_with_func_arg(other_func)  # <- here IDE must complain that only func_as_param is expected
Asked By: Anton Samokat

||

Answers:

I don’t know if it’s just me that Python doesn’t make any changes to the type Annotations

Syntax :

def greeting(name: str) -> str:
    return 'Hello ' + name

you can find more here: https://docs.python.org/3/library/typing.html

Answered By: Nabil A Navab

normally this is done by creating your own type (class) … then any other function can inherit from it and will be of the same "type".


class my_functions:
    pass

class func_as_param_class(my_functions):

    @staticmethod
    def __call__():
        print("func_as_param called")

func_as_param = func_as_param_class() # create the callable function ....


def other_func():
    print("other_func called")


def func_with_func_arg(func: my_functions): # only my_functions are accepted.
# def func_with_func_arg(func: type(func_as_param)):
    print("func_with_func_arg called")
    func()


def test_func_with_func_arg():
    print("test_func_with_func_arg")
    func_with_func_arg(func_as_param)
    func_with_func_arg(other_func)  # <- here IDE must complain that only func_as_param is expected

in the above code, my IDE (pycharm) does complain that other_func is of the wrong type, this however doesn’t do any restriction at runtime, it only allows IDE linter and mypy to issue warning on violation.

Edit: removed the arguments by declaring the call function as static.

Answered By: Ahmed AEK

Frameworks expecting callback functions of specific signatures might be type hinted using Callable[[Arg1Type, Arg2Type], ReturnType]

You might want to use the Callable type. This might help https://docs.python.org/3/library/typing.html#callable

NOTE
Type annotations in Python are not make-or-break like in C. They’re optional chunks of syntax that we can add to make our code more explicit.
Erroneous type annotations will do nothing more than highlight the incorrect annotation in our code editor — no errors are ever raised due to annotations.
If thats necessary, you must do the checking by yourself.

Answered By: Tantary Tauseef
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.