Instance attributes are typically annotated on the class:
class Foo:
x: int
def __init__(self, x):
self.x = x
Foo(0).x
This works and mypy doesn't report any issues. However, when the instance attribute is a Callable then mypy starts complaining:
from typing import Callable
class Foo:
func: Callable[[], int]
def __init__(self, func):
self.func = func
Foo(lambda: 0).func()
I get the following error:
test.py:11: error: Attribute function "func" with type "Callable[[], int]" does not accept self argument
Found 1 error in 1 file (checked 1 source file)
Since this function is not defined on the class, but only stored in the instance dict, it won't be bound to the instance during attribute lookup (in short: the above snippet works). So I don't see why mypy would complain about this. Is there another way to type hint such instance-level functions?