microsoft/qlib · error · NotImplementedError

Please implement the `__sub__` method

Error message

Please implement the `__sub__` method

What it means

BaseSingleMetric.__sub__ is the abstract subtraction operator (metric - metric, metric - scalar). It raises NotImplementedError in the base class; SingleMetric provides the concrete element-wise implementation. Encountering it means the operand is a bare BaseSingleMetric or a subclass that did not override __sub__.

Source

Thrown at qlib/backtest/high_performance_ds.py:237

            keys/index is stock_id, value is the metric value.
            for example:
                SH600068    NaN
                SH600079    1.0
                SH600266    NaN
                           ...
                SZ300692    NaN
                SZ300719    NaN,
        """
        raise NotImplementedError(f"Please implement the `__init__` method")

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

View on GitHub (pinned to 79633dd950)

Solutions

  1. Operate on SingleMetric instances
  2. Implement __sub__(self, other) in your BaseSingleMetric subclass (NaN-aware element-wise subtraction, reindexing on the union of stock ids)
  3. Type-check operands before subtraction in generic report code

Example fix

# before
diff = BaseSingleMetric(a) - BaseSingleMetric(b)
# after
from qlib.backtest.high_performance_ds import SingleMetric
diff = SingleMetric(a) - SingleMetric(b)
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: m1 - m2 or m - 0.5 where m1/m is a BaseSingleMetric instance; PnL-style computations (value - cost) in custom reporting code that was handed a base-class-typed object.

Common situations: Writing custom cost/return metrics over qlib positions; subclasses implementing __rsub__ (which the base defines as other - self delegation path) but not __sub__; partial mocks in tests replacing only some operators.

Related errors


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