I am trying to use type hinting on a callback function/method which is passed as an argument. See the below example. The "function based" implementation works: Mypy reports the expected error.
error: Argument 1 to "main_with_callback" has incompatible type "Callable[[], Any]"; expected "Callable[[str], Any]"
If I do the same from inside a class. The error is not reported. It seems only the return type of the Callable definition is evaluated.
I can't see anything wrong. Anyone a suggestion ?
from typing import Callable, Any
# Functions with callback as argument.
def callback():
print("any arg")
def main_with_callback(callback: Callable[[str], Any]):
callback("this is the callback")
main_with_callback(callback)
# Class with callback as argument.
class A:
def __init__(self):
self.main_with_callback(self.the_callback)
def main_with_callback(self, _callback: Callable[[str], Any]):
_callback("this is the callback")
def the_callback(self):
print("called")
a = A()