Skip to content

Instantly share code, notes, and snippets.

@mbergal
Created August 25, 2021 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbergal/ebf3f29f702c676b68ace9714537e564 to your computer and use it in GitHub Desktop.
Save mbergal/ebf3f29f702c676b68ace9714537e564 to your computer and use it in GitHub Desktop.
from abc import abstractmethod
from typing import Generic, Protocol, TypeVar
T_cov = TypeVar("T", contravariant=True)
U_cov = TypeVar("U", contravariant=True)
class Metric(Protocol[T]):
@abstractmethod
def __call__(self, gt: T, pred: T) -> float:
...
class SomeMetric(Metric[int]):
def __call__(self, gt: int, pred: int) -> float:
return 0
class MetricsStructure(Generic[T_cov]):
m1: T_cov
m2: T_cov
class MetricCollection(Protocol[U_cov]):
def __init__(self, metrics: MetricsStructure[Metric[U]]):
self.__metrics = metrics
def __call__(self, gt: U_cov, pred: U_cov) -> MetricsStructure[float]:
return {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment