How to annotate a class decorator that extends a class with additional members?
I have the following example where my class decorator inserts an async new function that calls the provided class' ainit. This code runs correctly, but the type returned by optimizer is the original type, and new is missing. How can I improve annotation, if possible?
AinitArguments = ParamSpec("AinitArguments")
class Optimizerable(Protocol):
ainit: Callable[AinitArguments, None]
T = TypeVar("T", bound=Optimizerable)
def optimizer(cls: Type[T]) -> Type[T]:
@classmethod
async def new(cls: Type[T], *args, **kwargs) -> T:
self = cls.__new__(cls)
await self.ainit(*args, **kwargs)
return self
setattr(cls, "new", new)
return cls
@optimizer
class Myclass:
async def ainit(self, a: int, b: float) -> None:
pass # stub