Replies: 1 comment
-
If you can switch to composition instead of inheritance, I think you can make it work. Params = ParamSpec("Params")
Return = TypeVar("Return", covariant=True)
class ForwardProtocol(Protocol, Generic[Params, Return]):
def forward(self, *args: Params.args, **kwargs: Params.kwargs) -> Return:
...
class Wrapper(Generic[Params, Return]):
def __init__(self, impl: ForwardProtocol[Params, Return]) -> None:
self._impl = impl
def wrapper(self, *args: Params.args, **kwargs: Params.kwargs) -> Return:
return self._impl.forward(*args, **kwargs) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a pattern that looks something like this:
and would like
wrapper
to have the same type annotation asforward
(this is a common pattern that is used in PyTorch and TensorFlow when defining custom modules, for instance).I can almost get what I want using
ParamSpec
andTypeVar
like:But this is not quite what I want:
P
andT
again in the inheritance call (e.g.class Subclass(BaseClass[[int], str])
along withdef forward(self, x: int, /) -> str
Is there a good way to support this pattern?
Beta Was this translation helpful? Give feedback.
All reactions