microsoft/qlib · error · NotImplementedError
Please implement the `__len__` method
Error message
Please implement the `__len__` method
What it means
BaseSingleMetric.__len__ is abstract: the base class defines the protocol (returning the number of stocks in the metric vector) but raises NotImplementedError. SingleMetric implements it. Calling len() on a raw BaseSingleMetric — including implicitly via iteration, list(), or emptiness checks — raises this error.
Source
Thrown at qlib/backtest/high_performance_ds.py:258
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")
def count(self) -> int:
"""Return the count of the single metric, NaN is not included."""
raise NotImplementedError(f"Please implement the `count` method")
def abs(self) -> BaseSingleMetric:
raise NotImplementedError(f"Please implement the `abs` method")
@property
def empty(self) -> bool:
"""If metric is empty, return True."""View on GitHub (pinned to 79633dd950)
Solutions
- Use SingleMetric, where __len__ returns the length of the underlying stock-indexed Series
- Implement __len__(self) in your subclass (e.g. return len(self.storage))
- For emptiness checks use the .empty property contract instead of len() == 0
Example fix
# before n = len(BaseSingleMetric(s)) # after from qlib.backtest.high_performance_ds import SingleMetric n = len(SingleMetric(s))
Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.backtest.high_performance_ds import BaseSingleMetric assert type(m).__len__ is not BaseSingleMetric.__len__, "object does not implement __len__"
Type guard
def supports_len(m) -> bool:
from qlib.backtest.high_performance_ds import BaseSingleMetric
return isinstance(m, BaseSingleMetric) and type(m).__len__ is not BaseSingleMetric.__len__ Prevention
- Use SingleMetric; len() then works naturally
- For emptiness checks prefer the .empty property over len() == 0
- Avoid list()/iteration over metric objects; iterate m.storage.index
When it happens
Trigger: len(m), list(m), `for s in m`, or bool(m) on a bare BaseSingleMetric; report code counting covered stocks; subclasses that store data but forget to define __len__.
Common situations: Coverage checks like len(metric) vs universe size; converting metrics to lists in logging; generic Python builtins (any/all/iter) that call __len__/__iter__ indirectly.
Related errors
- Please implement the `__init__` method
- Please implement the `__add__` method
- Please implement the `__sub__` method
- Please implement the `__rsub__` method
- Please implement the `__mul__` method
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/43b6d9599ad65eed.
Report an issue: GitHub.