1

Context

Assume a function taking in a function like this:

def some_func(
        parameter_1: str
        , func: function
    ):
    pass

As can be seen func shall be a function passed to some_func.

Anyhow, func shall be a function taking in a specifc parameter-type:

def func(
        specific_parameter_of_type_string: str
    ):
    pass

Question

How do I declare in some_func() that the parameter func shall be a function which takes in a str?

I looked into the typing-module but did not find the solution to my question. Anyhow, I think it should be solvable with it...

The result would resemble something like this I assume:

import typing

def some_func(
        parameter_1: str
        , func: typing.Function[str] # Example! This does not exist in typing
    ):
    pass
5
  • @not_speshal: Sorry, but this is not a solution to this problem - Casting is something entirely different than type hinting. Commented Feb 15, 2022 at 18:08
  • "func shall be a function taking in a specifc parameter-type" Commented Feb 15, 2022 at 18:10
  • 1
    Are you looking for Callable? Commented Feb 15, 2022 at 18:15
  • @not_speshal: Yes exactly, and this is not declared by casting but by typing :) Commented Feb 15, 2022 at 18:16
  • @MisterMiyagi: Yes I was :) Encountered this also in the moment you wrote :D Commented Feb 15, 2022 at 18:17

1 Answer 1

0

Reading the typing documentation more carefully I encountered that functions are actually referred to as typing.Callables as can be read here.

Thus, this can be used to specify both, the return type and input arguments:

Callable[[Arg1Type, Arg2Type], ReturnType]

The solution is then:

import typing

def some_func(
        parameter_1: str
        , func: typing.Callable[[str], None]
    ):
    pass
Sign up to request clarification or add additional context in comments.

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.