Com o seguinte exemplo:
from typing import Callable, Generic, Type, TypeVar
ThetaType = TypeVar('ThetaType', bound=int)
XType = TypeVar('XType', bound=int)
class IteratedFunction(Generic[ThetaType, XType]):
def find_fixed_point(self,
theta: ThetaType,
x_init: XType) -> XType:
return x_init
def combinator(
iterated_function_cls: Type[
IteratedFunction[ThetaType, XType]]) -> Callable[
[IteratedFunction[ThetaType, XType]], XType]:
old_find_fixed_point = iterated_function_cls.find_fixed_point
def new_find_fixed_point(
iterated_function: IteratedFunction[ThetaType, XType],
theta: ThetaType,
x_init: XType) -> XType:
return old_find_fixed_point(iterated_function, theta, x_init)
return new_find_fixed_point
MyPy diz:
a.py:25: error: Incompatible return value type (got "XType", expected "XType")
a.py:25: error: Argument 1 has incompatible type "IteratedFunction[ThetaType, XType]"; expected "IteratedFunction[ThetaType, XType]"
a.py:25: error: Argument 2 has incompatible type "ThetaType"; expected "ThetaType"
a.py:25: error: Argument 3 has incompatible type "XType"; expected "XType"
a.py:27: error: Incompatible return value type (got "Callable[[IteratedFunction[ThetaType, XType], ThetaType, XType], XType]", expected "Callable[[IteratedFunction[ThetaType, XType]], XType]")
new_find_fixed_point
como uma função genérica com sua própria instanciação separada deThetaType
eXType
.