{"record":{"id":"8ef7dc5cee9add32","repo":"microsoft/qlib","slug":"please-implement-the-sum-method","errorCode":null,"errorMessage":"Please implement the `sum` method","messagePattern":"Please implement the `sum` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":261,"sourceCode":"        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.\"\"\"\n\n        raise NotImplementedError(f\"Please implement the `empty` method\")\n","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L243-L279","documentation":"BaseSingleMetric.sum() is the abstract reduction that should return the total of the metric values across stocks (as a float). The base class raises NotImplementedError; SingleMetric implements the concrete NaN-aware sum. Calling .sum() on a bare BaseSingleMetric raises this error.","triggerScenarios":"m.sum() where m is a raw BaseSingleMetric; portfolio-level aggregation of per-stock metrics (total market value, total profit); subclasses implementing mean but not sum; code using sum(metric) which routes through __radd__/__add__ instead of .sum() and fails differently.","commonSituations":"Building performance reports (total return, total exposure); summary statistics in custom evaluators; incomplete custom metric backends.","solutions":["Use SingleMetric.sum(), which sums the underlying values while handling NaN","Implement sum(self) in your subclass (e.g. return float(self.storage.sum()) if pandas-backed)","For mixed pipelines, dispatch on hasattr(type(m), 'sum') is not enough — check the method is not the base stub"],"exampleFix":"# before\ntotal = BaseSingleMetric(values).sum()\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\ntotal = SingleMetric(values).sum()","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).sum is not BaseSingleMetric.sum, \"object does not implement sum()\"","typeGuard":"def supports_sum(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).sum is not BaseSingleMetric.sum","tryCatchPattern":null,"preventionTips":["Use SingleMetric for reductions","Implement sum/count/mean together in subclasses so derived stats stay consistent","Prefer m.sum() over sum(m) — the latter exercises operator overloading instead"],"tags":["python","qlib","abstract-method","aggregation","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}