{"record":{"id":"c8df5bcd7432a040","repo":"microsoft/qlib","slug":"please-implement-the-sub-method","errorCode":null,"errorMessage":"Please implement the `__sub__` method","messagePattern":"Please implement the `__sub__` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":237,"sourceCode":"            keys/index is stock_id, value is the metric value.\n            for example:\n                SH600068    NaN\n                SH600079    1.0\n                SH600266    NaN\n                           ...\n                SZ300692    NaN\n                SZ300719    NaN,\n        \"\"\"\n        raise NotImplementedError(f\"Please implement the `__init__` method\")\n\n    def __add__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__add__` method\")\n\n    def __radd__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        return self + other\n\n    def __sub__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        raise NotImplementedError(f\"Please implement the `__sub__` method\")\n\n    def __rsub__(self, other: Union[BaseSingleMetric, int, float]) -> BaseSingleMetric:\n        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\")","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L219-L255","documentation":"BaseSingleMetric.__sub__ is the abstract subtraction operator (metric - metric, metric - scalar). It raises NotImplementedError in the base class; SingleMetric provides the concrete element-wise implementation. Encountering it means the operand is a bare BaseSingleMetric or a subclass that did not override __sub__.","triggerScenarios":"m1 - m2 or m - 0.5 where m1/m is a BaseSingleMetric instance; PnL-style computations (value - cost) in custom reporting code that was handed a base-class-typed object.","commonSituations":"Writing custom cost/return metrics over qlib positions; subclasses implementing __rsub__ (which the base defines as other - self delegation path) but not __sub__; partial mocks in tests replacing only some operators.","solutions":["Operate on SingleMetric instances","Implement __sub__(self, other) in your BaseSingleMetric subclass (NaN-aware element-wise subtraction, reindexing on the union of stock ids)","Type-check operands before subtraction in generic report code"],"exampleFix":"# before\ndiff = BaseSingleMetric(a) - BaseSingleMetric(b)\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\ndiff = SingleMetric(a) - SingleMetric(b)","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).__sub__ is not BaseSingleMetric.__sub__, \"object does not implement __sub__\"","typeGuard":"def supports_sub(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).__sub__ is not BaseSingleMetric.__sub__","tryCatchPattern":null,"preventionTips":["Use SingleMetric for subtraction (PnL, cost differences)","Implement __sub__ and __rsub__ as a pair in subclasses","Align stock ids (reindex on the union) before subtracting two metrics"],"tags":["python","qlib","operator-overloading","abstract-method","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}