Skip to content

how to make protocol compatible with bounded TypeVar #1865

Answered by Daverball
ViktorSky asked this question in Q&A
Discussion options

You must be logged in to vote

Alternatively you can create a Protocol for the whole TraceType, if you don't actually care that it's an actual instance of TraceType, but rather just has the correct shape:

from __future__ import annotations

import typing

T = typing.TypeVar("T")
T_co = typing.TypeVar("T_co", covariant=True)
TraceLikeT = typing.TypeVar("TraceLikeT", bound="TraceLike[typing.Any]")

class TraceType: # no editable
    def from_tensors(self, tensors: list[typing.Any]) -> typing.Any: ...
    def placeholder_value(self) -> typing.Any: ...

class TraceLike(typing.Protocol[T_co]):
    def from_tensors(self, tensors: list[typing.Any]) -> typing.Any: ...
    def placeholder_value(self) -> T_co: ...

class MyTrace…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Daverball
Comment options

Answer selected by ViktorSky
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants