5

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
7
  • 1
    what do you mean by the compiler ? do you mean the IDE ? as in the linter will underline your code as error ? or do you mean python interpreter will raise an error ? as in during runtime an error will be raised ? you cannot normally raise errors during compilation time in python ... and by compilation i mean compiling .py to .pyc .... Commented Oct 7, 2022 at 18:06
  • 1
    It is crucial to understand, type annotations are not enforced by the Python runtime itself Commented Oct 7, 2022 at 18:19
  • 1
    Also important to understand, func: func_as_param is not a valid type annotation (although, it won't produce any sort of error and the code will execute just fine). Commented Oct 7, 2022 at 18:19
  • 1
    And there is no way to express "only pass these particular functions, or functions from this module". Probably you should just defined your own type to do this, atlhough again, very important to understand, this doesn't actually affect what can be passed to the function Commented Oct 7, 2022 at 18:20
  • 1
    @AntonSamokat yes, that would be the straightforward way to do this. Commented Oct 7, 2022 at 18:26

3 Answers 3

6

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#annotating-callable-objects

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.