{"record":{"id":"fad9216eff4e8747","repo":"microsoft/qlib","slug":"please-implement-the-truediv-method","errorCode":null,"errorMessage":"Please implement the `__truediv__` method","messagePattern":"Please implement the `__truediv__` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":246,"sourceCode":"        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\")\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\")","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L228-L264","documentation":"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.","triggerScenarios":"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__.","commonSituations":"Normalizing exposure or weight metrics; computing ratios such as turnover = traded / total; custom indicator classes that inherit BaseSingleMetric and miss the division operator.","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"],"exampleFix":"# before\nweight = BaseSingleMetric(value) / total_value\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nweight = SingleMetric(value) / total_value","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).__truediv__ is not BaseSingleMetric.__truediv__, \"object does not implement __truediv__\"","typeGuard":"def supports_truediv(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).__truediv__ is not BaseSingleMetric.__truediv__","tryCatchPattern":null,"preventionTips":["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"],"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"}