microsoft/qlib · error · NotImplementedError

Please implement the `__eq__` method

Error message

Please implement the `__eq__` method

What it means

BaseSingleMetric.__eq__ is declared abstract. Unlike normal Python objects, equality here is meant to return a new metric of element-wise boolean comparisons (return annotation is BaseSingleMetric), not a plain bool. The base class has no implementation, so == on a raw BaseSingleMetric raises NotImplementedError instead of comparing. SingleMetric implements it.

Source

Thrown at qlib/backtest/high_performance_ds.py:249

        raise NotImplementedError(f"Please implement the `__add__` method")

    def __radd__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:
        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."""

View on GitHub (pinned to 79633dd950)

Solutions

  1. Compare SingleMetric instances, which return element-wise metric results
  2. Implement __eq__ in your subclass returning a metric of booleans (and keep the __hash__/__eq__ contract in mind)
  3. For equality checks in tests, compare .storage (the underlying Series/dict) or use .sum()/.count() aggregations

Example fix

# before
mask = BaseSingleMetric(s) == 0.0
# after
from qlib.backtest.high_performance_ds import SingleMetric
mask = SingleMetric(s) == 0.0  # returns SingleMetric of booleans
Defensive patterns

Strategy: type-guard

Validate before calling

from qlib.backtest.high_performance_ds import BaseSingleMetric
assert type(m).__eq__ is not BaseSingleMetric.__eq__, "object does not implement __eq__"

Type guard

def supports_eq(m) -> bool:
    from qlib.backtest.high_performance_ds import BaseSingleMetric
    return isinstance(m, BaseSingleMetric) and type(m).__eq__ is not BaseSingleMetric.__eq__

Prevention

When it happens

Trigger: m == 0.0 or m1 == m2 where m is a bare BaseSingleMetric; filtering code that builds boolean masks from metric comparisons; using BaseSingleMetric instances in set/dict membership tests (which call __eq__ via __hash__ machinery); incomplete subclasses that override comparison operators partially.

Common situations: Signal-masking code (value == target_price); tests asserting metric equality; custom boolean masks in strategy logic. Note: because __eq__ is overridden in subclasses, __hash__ behavior may change — avoid hashing metrics.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/47be01eebfc99a4c. Report an issue: GitHub.