microsoft/qlib · error · NotImplementedError

Please implement the `__init__` method

Error message

Please implement the `__init__` method

What it means

BaseSingleMetric is the abstract base for per-stock metric vectors (index = stock_id) used in qlib's high-performance account/position metrics. Its __init__(metric: Union[dict, pd.Series]) only raises NotImplementedError; concrete classes such as SingleMetric (qlib/backtest/high_performance_ds.py:436) implement storage. Instantiating BaseSingleMetric directly, or subclassing it without overriding __init__, raises this at construction.

Source

Thrown at qlib/backtest/high_performance_ds.py:228

    The following methods are used for computing metrics in one indicator.
    """

    def __init__(self, metric: Union[dict, pd.Series]):
        """Single data structure for each metric.

        Parameters
        ----------
        metric : Union[dict, pd.Series]
            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")

View on GitHub (pinned to 79633dd950)

Solutions

  1. Instantiate SingleMetric instead of BaseSingleMetric
  2. If subclassing BaseSingleMetric, implement __init__(self, metric) storing the dict/Series (see SingleMetric.__init__ for the pattern)
  3. Guard factories: assert type(obj) is not BaseSingleMetric before returning

Example fix

# before
m = BaseSingleMetric(pd.Series({"SH600000": 1.0}))
# after
from qlib.backtest.high_performance_ds import SingleMetric
m = SingleMetric(pd.Series({"SH600000": 1.0}))
Defensive patterns

Strategy: type-guard

Validate before calling

from qlib.backtest.high_performance_ds import BaseSingleMetric, SingleMetric
assert type(metric) is not BaseSingleMetric, "instantiate SingleMetric, not BaseSingleMetric"

Type guard

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

Prevention

When it happens

Trigger: BaseSingleMetric({...}) or BaseSingleMetric(pd.Series(...)); a custom metric class inheriting BaseSingleMetric without defining __init__; factory functions typed to return BaseSingleMetric that accidentally return a bare instance.

Common situations: Extending qlib's metric layer with a new backend (e.g. dict-backed or numpy-backed metrics) and forgetting __init__; test scaffolding that constructs the base class to inspect its interface.

Related errors


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