I have a function that takes other functions as argument, There are some possible options that the function can take, and I want to annotate them properly using Enums and type hints:
This is what I tried:
from typing import Callable
from enum import Enum
def test1():
print('hi')
def test2():
print('hi2')
class FuncEnum(Enum):
FUN1 = test1
FUN2 = test2
# option 1, mypy will complain because FuncEnum.FUN1 is not in the Callable type
def my_annotated_function(func: Callable = FuncEnum.FUN1):
func()
# option 2, func is not callable. as it is FuncEnum type (not Callable)
def my_annotated_function(func: FuncEnum = FuncEnum.FUN1):
func()
The code works the problem is only that the IDE/mypy raises a warning with both syntax.
I also tried sub-classing Callable, but it doesn't seem to work:
class FuncEnum(Callable, Enum):
FUN1 = test1
FUN2 = test2
Somehow the lintern/mypy seems to be lost with this kind of annotations, is there any work around?
Notice that the calls:
my_annotated_function(FuncEnum.FUN1) # should be a valid input
def random_function():
pass
my_annotated_function(random_function) # should be raised by mypy