As @jonrsharpe noted in a comment, this can be done with collections.abc.Callable:
from collections.abc import Callable
def my_function(func: Callable):
Note: Callable on its own is equivalent to Callable[..., Any].
Such a Callable takes any number and type of arguments (...) and returns a value of any type (Any). If this is too unconstrained, one may also specify the types of the input argument list and return type.
For example, given:
def sum(a: int, b: int) -> int: return a+b
The corresponding annotation is:
Callable[[int, int], int]
That is, the parameters are sub-scripted in the outer subscription with the return type as the second element in the outer subscription. In general:
Callable[[ParamType1, ParamType2, ..., ParamTypeN], ReturnType]
Answer from Dimitris Fasarakis Hilliard on Stack OverflowAs @jonrsharpe noted in a comment, this can be done with collections.abc.Callable:
from collections.abc import Callable
def my_function(func: Callable):
Note: Callable on its own is equivalent to Callable[..., Any].
Such a Callable takes any number and type of arguments (...) and returns a value of any type (Any). If this is too unconstrained, one may also specify the types of the input argument list and return type.
For example, given:
def sum(a: int, b: int) -> int: return a+b
The corresponding annotation is:
Callable[[int, int], int]
That is, the parameters are sub-scripted in the outer subscription with the return type as the second element in the outer subscription. In general:
Callable[[ParamType1, ParamType2, ..., ParamTypeN], ReturnType]
My specific use case for wanting this functionality was to enable rich code completion in PyCharm. Using Callable didn't cause PyCharm to suggest that the object had a .__code__ attribute, which is what I wanted, in this case.
I stumbled across the types module and...
from types import FunctionType
allowed me to annotate an object with FunctionType and, voilà, PyCharm now suggests my object has a .__code__ attribute.
The OP wasn't clear on why this type hint was useful to them. Callable certainly works for anything that implements .__call__() but for further interface clarification, I submit the types module.
Bummer that Python needed two very similar modules.
What is the point of type hinting when Python doesn't even respect it!?
Why Type Hinting Sucks!
Type hinting functions with multipe return types including None
Annotating args and kwargs in Python
So infuriating because I feel like my function is lying to me when it ends up letting other objects through the parameter.
Now every function I created needs an instance check which is unintuitive, verbose, and easily forgotten.
def wtf(string: str, integer:int): return 'TYPE HINT DOESNT DO ANYTHING' print( wtf( 123, 'wtf') ) >> 'TYPE HINT DOESNT DO ANYTHING'
EDIT:
Turns out it is a noob mistake. Type hint only functions as a signal for IDE, but developers get to do whatever they want to your parameter.
If you really must enforce it, you got to have the line
if not isinstance(string, str): raise TypeError()
Still, I don't like that this behavior isn't taught until it happens. So much time wasted on debugging production code when you take for granted your function is accepting only restricted data when it doesn't.