-
While creating stubs files, I ran into this problem import typing
T = typing.TypeVar("T")
T_co = typing.TypeVar("T_co", covariant=True)
TraceTypeT = typing.TypeVar("TraceTypeT", bound="TraceType")
class TraceType: # no editable
def from_tensors(self, tensors: list[typing.Any]) -> typing.Any: ...
def placeholder_value(self) -> typing.Any: ...
# Protocol class "SupportsPlaceholderValue" cannot derive from non-Protocol class "TraceType"
class SupportsPlaceholderValue(TraceType, typing.Protocol[T_co]): # <-- error
def placeholder_value(self) -> T_co: ...
class MyTraceType(typing.Generic[TraceTypeT], TraceType):
def __init__(self, *components: TraceTypeT) -> None:
self.components: tuple[TraceTypeT, ...]
# error: SupportsPlaceholderValue[T@result]" is not assignable to "TraceType"
def result(self: MyTraceType[SupportsPlaceholderValue[T]]) -> list[T]: # ???
# .py implementation
return [c.placeholder_value() for c in self.components] |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The best you can do is provide a generic from __future__ import annotations
import typing
T = typing.TypeVar("T")
T_co = typing.TypeVar("T_co", covariant=True)
TraceTypeT = typing.TypeVar("TraceTypeT", bound="TraceType")
class TraceType: # no editable
def from_tensors(self, tensors: list[typing.Any]) -> typing.Any: ...
def placeholder_value(self) -> typing.Any: ...
class GenericTraceType(TraceType, typing.Generic[T_co]):
if typing.TYPE_CHECKING:
def placeholder_value(self) -> T_co: ...
class MyTraceType(typing.Generic[TraceTypeT], TraceType):
def __init__(self, *components: TraceTypeT) -> None:
self.components: tuple[TraceTypeT, ...]
@typing.overload
def result(self: MyTraceType[GenericTraceType[T]]) -> list[T]: ...
@typing.overload
def result(self) -> list[typing.Any]: ...
def result(self) -> list[typing.Any]:
# .py implementation
return [c.placeholder_value() for c in self.components] |
Beta Was this translation helpful? Give feedback.
Alternatively you can create a
Protocol
for the wholeTraceType
, if you don't actually care that it's an actual instance ofTraceType
, but rather just has the correct shape: