microsoft/qlib · error · NotImplementedError
Please implement the `__gt__` method
Error message
Please implement the `__gt__` method
What it means
BaseSingleMetric.__gt__ is the abstract greater-than operator, intended to return an element-wise boolean metric (return type BaseSingleMetric), not a bool. The base class raises NotImplementedError; SingleMetric provides the implementation. Applying > to a bare BaseSingleMetric raises this error.
Source
Thrown at qlib/backtest/high_performance_ds.py:252
return self + other
def __sub__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__sub__` method")
def __rsub__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__rsub__` method")
def __mul__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__mul__` method")
def __truediv__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__truediv__` method")
def __eq__(self, other: object) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__eq__` method")
def __gt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__gt__` method")
def __lt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `__lt__` method")
def __len__(self) -> int:
raise NotImplementedError(f"Please implement the `__len__` method")
def sum(self) -> float:
raise NotImplementedError(f"Please implement the `sum` method")
def mean(self) -> float:
raise NotImplementedError(f"Please implement the `mean` method")
def count(self) -> int:
"""Return the count of the single metric, NaN is not included."""
raise NotImplementedError(f"Please implement the `count` method")
View on GitHub (pinned to 79633dd950)
Solutions
- Use SingleMetric for comparisons; convert the result with .astype or use its storage when a pandas boolean Series is needed
- Implement __gt__ in your subclass returning type(self)(self.storage > other)
- Sort/compare the underlying .storage Series rather than metric objects
Example fix
# before buy = BaseSingleMetric(score) > 0.0 # after from qlib.backtest.high_performance_ds import SingleMetric buy = SingleMetric(score) > 0.0 # SingleMetric of booleans
Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.backtest.high_performance_ds import BaseSingleMetric assert type(m).__gt__ is not BaseSingleMetric.__gt__, "object does not implement __gt__"
Type guard
def supports_gt(m) -> bool:
from qlib.backtest.high_performance_ds import BaseSingleMetric
return isinstance(m, BaseSingleMetric) and type(m).__gt__ is not BaseSingleMetric.__gt__ Prevention
- Use SingleMetric when thresholding into boolean masks
- Never sort metric objects; sort m.storage instead
- Implement __gt__ and __lt__ together in subclasses
When it happens
Trigger: m > threshold or m1 > m2 on a raw BaseSingleMetric; strategy filter code like (momentum > 0) to build tradable universes; sorted()/max() calls that invoke __gt__ on metric objects; subclasses implementing __lt__ but not __gt__.
Common situations: Thresholding factor scores into buy/sell masks; ranking code that accidentally sorts metric objects instead of their underlying Series; partial operator coverage in user subclasses.
Related errors
- Please implement the `__lt__` method
- Please implement the `__add__` method
- Please implement the `__sub__` method
- Please implement the `__rsub__` method
- Please implement the `__mul__` method
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/3afbeb43023befe5.
Report an issue: GitHub.