{"record":{"id":"05b02f016ef5650e","repo":"microsoft/qlib","slug":"please-implement-the-init-method","errorCode":null,"errorMessage":"Please implement the `__init__` method","messagePattern":"Please implement the `__init__` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":228,"sourceCode":"    The following methods are used for computing metrics in one indicator.\n    \"\"\"\n\n    def __init__(self, metric: Union[dict, pd.Series]):\n        \"\"\"Single data structure for each metric.\n\n        Parameters\n        ----------\n        metric : Union[dict, pd.Series]\n            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\")","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L210-L246","documentation":"BaseSingleMetric is the abstract base for per-stock metric vectors (index = stock_id) used in qlib's high-performance account/position metrics. Its __init__(metric: Union[dict, pd.Series]) only raises NotImplementedError; concrete classes such as SingleMetric (qlib/backtest/high_performance_ds.py:436) implement storage. Instantiating BaseSingleMetric directly, or subclassing it without overriding __init__, raises this at construction.","triggerScenarios":"BaseSingleMetric({...}) or BaseSingleMetric(pd.Series(...)); a custom metric class inheriting BaseSingleMetric without defining __init__; factory functions typed to return BaseSingleMetric that accidentally return a bare instance.","commonSituations":"Extending qlib's metric layer with a new backend (e.g. dict-backed or numpy-backed metrics) and forgetting __init__; test scaffolding that constructs the base class to inspect its interface.","solutions":["Instantiate SingleMetric instead of BaseSingleMetric","If subclassing BaseSingleMetric, implement __init__(self, metric) storing the dict/Series (see SingleMetric.__init__ for the pattern)","Guard factories: assert type(obj) is not BaseSingleMetric before returning"],"exampleFix":"# before\nm = BaseSingleMetric(pd.Series({\"SH600000\": 1.0}))\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nm = SingleMetric(pd.Series({\"SH600000\": 1.0}))","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric, SingleMetric\nassert type(metric) is not BaseSingleMetric, \"instantiate SingleMetric, not BaseSingleMetric\"","typeGuard":"def is_concrete_metric(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).__init__ is not BaseSingleMetric.__init__","tryCatchPattern":null,"preventionTips":["Treat BaseSingleMetric as an interface; always construct SingleMetric","In subclasses, implement __init__ first — everything else depends on storage it sets up","Add a small test that constructs every class you ship"],"tags":["python","qlib","abstract-method","not-implemented","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}