microsoft/qlib · error · NotImplementedError

Please implement the `__mul__` method

Error message

Please implement the `__mul__` method

What it means

BaseSingleMetric.__mul__ is the abstract multiplication operator for per-stock metric vectors. The base class only declares the protocol; SingleMetric supplies the element-wise implementation. The error appears only when multiplying a bare BaseSingleMetric (or an incomplete subclass) by another metric or a scalar.

Source

Thrown at qlib/backtest/high_performance_ds.py:243

                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")

    def __len__(self) -> int:
        raise NotImplementedError(f"Please implement the `__len__` method")

    def sum(self) -> float:
        raise NotImplementedError(f"Please implement the `sum` method")

View on GitHub (pinned to 79633dd950)

Solutions

  1. Use SingleMetric for multiplication
  2. Implement __mul__(self, other) in your subclass following SingleMetric's NaN-aware element-wise pattern
  3. Validate operand types before multiplying in generic pipelines

Example fix

# before
notional = BaseSingleMetric(price) * 100
# after
from qlib.backtest.high_performance_ds import SingleMetric
notional = SingleMetric(price) * 100
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: m * 2.0, m1 * m2, or m * position_metric where m is a raw BaseSingleMetric; weighting code such as weight * price inside custom indicators; subclasses defining __rmul__ without __mul__.

Common situations: Scaling account metrics (e.g. applying leverage or FX multipliers); element-wise products of two metric vectors (price * shares); partial operator implementations in user subclasses.

Related errors


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