microsoft/qlib · error · NotImplementedError
Please implement the `__truediv__` method
Error message
Please implement the `__truediv__` method
What it means
BaseSingleMetric.__truediv__ is the abstract true-division operator (the / operator). The base class raises NotImplementedError; SingleMetric implements the concrete element-wise division. Dividing a raw BaseSingleMetric by a scalar or another metric triggers the error.
Source
Thrown at qlib/backtest/high_performance_ds.py:246
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")
def mean(self) -> float:
raise NotImplementedError(f"Please implement the `mean` method")View on GitHub (pinned to 79633dd950)
Solutions
- Use SingleMetric, whose __truediv__ handles scalar and metric divisors element-wise
- Implement __truediv__(self, other) in your subclass (guard divide-by-zero with np.errstate or fillna as SingleMetric does)
- Type-check before dividing in shared report utilities
Example fix
# before weight = BaseSingleMetric(value) / total_value # after from qlib.backtest.high_performance_ds import SingleMetric weight = SingleMetric(value) / total_value
Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.backtest.high_performance_ds import BaseSingleMetric assert type(m).__truediv__ is not BaseSingleMetric.__truediv__, "object does not implement __truediv__"
Type guard
def supports_truediv(m) -> bool:
from qlib.backtest.high_performance_ds import BaseSingleMetric
return isinstance(m, BaseSingleMetric) and type(m).__truediv__ is not BaseSingleMetric.__truediv__ Prevention
- Use SingleMetric for ratio metrics (weights, returns)
- Guard divisors: check (m_divisor == 0).sum() on the underlying storage before dividing
- Implement __truediv__ alongside __mul__ in custom subclasses
When it happens
Trigger: m / total or m1 / m2 on a BaseSingleMetric instance; normalization code computing weights (value / portfolio_value); return calculations (profit / cost); subclasses implementing __mul__ but not __truediv__.
Common situations: Normalizing exposure or weight metrics; computing ratios such as turnover = traded / total; custom indicator classes that inherit BaseSingleMetric and miss the division operator.
Related errors
- Please implement the `__add__` method
- Please implement the `__sub__` method
- Please implement the `__rsub__` method
- Please implement the `__mul__` method
- Please implement the `__eq__` method
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/fad9216eff4e8747.
Report an issue: GitHub.