{"record":{"id":"43b6d9599ad65eed","repo":"microsoft/qlib","slug":"please-implement-the-len-method","errorCode":null,"errorMessage":"Please implement the `__len__` method","messagePattern":"Please implement the `__len__` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":258,"sourceCode":"        raise NotImplementedError(f\"Please implement the `__rsub__` method\")\n\n    def __mul__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__mul__` method\")\n\n    def __truediv__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__truediv__` method\")\n\n    def __eq__(self, other: object) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__eq__` method\")\n\n    def __gt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__gt__` method\")\n\n    def __lt__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__lt__` method\")\n\n    def __len__(self) -> int:\n        raise NotImplementedError(f\"Please implement the `__len__` method\")\n\n    def sum(self) -> float:\n        raise NotImplementedError(f\"Please implement the `sum` method\")\n\n    def mean(self) -> float:\n        raise NotImplementedError(f\"Please implement the `mean` method\")\n\n    def count(self) -> int:\n        \"\"\"Return the count of the single metric, NaN is not included.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `count` method\")\n\n    def abs(self) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `abs` method\")\n\n    @property\n    def empty(self) -> bool:\n        \"\"\"If metric is empty, return True.\"\"\"","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L240-L276","documentation":"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.","triggerScenarios":"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__.","commonSituations":"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.","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"],"exampleFix":"# before\nn = len(BaseSingleMetric(s))\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nn = len(SingleMetric(s))","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).__len__ is not BaseSingleMetric.__len__, \"object does not implement __len__\"","typeGuard":"def supports_len(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).__len__ is not BaseSingleMetric.__len__","tryCatchPattern":null,"preventionTips":["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"],"tags":["python","qlib","abstract-method","len","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}