microsoft/qlib · error · NotImplementedError
Please implement the `__rsub__` method
Error message
Please implement the `__rsub__` method
What it means
BaseSingleMetric.__rsub__ implements reflected subtraction (other - self). Unlike __radd__ (which the base class implements as self + other), __rsub__ is left abstract because operand order matters: the base class cannot compute scalar-minus-metric. Only SingleMetric (line 436) implements it, so 1.0 - base_metric on a bare BaseSingleMetric raises NotImplementedError.
Source
Thrown at qlib/backtest/high_performance_ds.py:240
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")
def __len__(self) -> int:
raise NotImplementedError(f"Please implement the `__len__` method")View on GitHub (pinned to 79633dd950)
Solutions
- Use SingleMetric, which implements both __sub__ and __rsub__
- In custom subclasses, implement __rsub__ as e.g. type(self)(other - self.storage) mirroring SingleMetric
- Rewrite scalar - m as m.__rsub__(scalar) only after confirming a concrete implementation exists, or compute (m * -1) + other
Example fix
# before inv = 1.0 - BaseSingleMetric(s) # after from qlib.backtest.high_performance_ds import SingleMetric inv = 1.0 - SingleMetric(s)
Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.backtest.high_performance_ds import BaseSingleMetric assert type(m).__rsub__ is not BaseSingleMetric.__rsub__, "object does not implement __rsub__"
Type guard
def supports_rsub(m) -> bool:
from qlib.backtest.high_performance_ds import BaseSingleMetric
return isinstance(m, BaseSingleMetric) and type(m).__rsub__ is not BaseSingleMetric.__rsub__ Prevention
- Prefer putting the metric on the left (m - scalar) so __rsub__ is never needed
- Implement __rsub__ whenever you implement __sub__ — the base class does not provide it (unlike __radd__)
- Use SingleMetric which implements both directions
When it happens
Trigger: Expressions like 1.0 - m or np.float64(x) - m where m is a raw BaseSingleMetric; broadcasting code (numpy, pandas ops) that triggers the reflected operator because the left operand's __sub__ returns NotImplemented; custom subclasses that override __sub__ but forget __rsub__.
Common situations: Writing inverse/excess-return metrics (benchmark - portfolio); mixing metric objects with numpy scalars; subclassing BaseSingleMetric and implementing only the forward operators.
Related errors
- Please implement the `__add__` method
- Please implement the `__sub__` method
- Please implement the `__mul__` method
- Please implement the `__truediv__` method
- Please implement the `__eq__` method
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/77335ed3dafba0cc.
Report an issue: GitHub.