{"record":{"id":"553c49b497a6e558","repo":"microsoft/qlib","slug":"please-implement-the-generate-method-553c49","errorCode":null,"errorMessage":"Please implement the `_generate` method","messagePattern":"Please implement the `_generate` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/workflow/record_temp.py","lineNumber":245,"sourceCode":"                logger.info(\"The results has previously generated, Generation skipped.\")\n                return\n\n        try:\n            self.check()\n        except FileNotFoundError:\n            logger.warning(\"The dependent data does not exists. Generation skipped.\")\n            return\n        artifact_dict = self._generate(*args, **kwargs)\n        if isinstance(artifact_dict, dict):\n            self.save(**artifact_dict)\n        return artifact_dict\n\n    def _generate(self, *args, **kwargs) -> Dict[str, object]:\n        \"\"\"\n        Run the concrete generating task, return the dictionary of the generated results.\n        The caller method will save the results to the recorder.\n        \"\"\"\n        raise NotImplementedError(f\"Please implement the `_generate` method\")\n\n\nclass HFSignalRecord(SignalRecord):\n    \"\"\"\n    This is the Signal Analysis Record class that generates the analysis results such as IC and IR. This class inherits the ``RecordTemp`` class.\n    \"\"\"\n\n    artifact_path = \"hg_sig_analysis\"\n    depend_cls = SignalRecord\n\n    def __init__(self, recorder, **kwargs):\n        super().__init__(recorder=recorder)\n\n    def generate(self):\n        pred = self.load(\"pred.pkl\")\n        raw_label = self.load(\"label.pkl\")\n        long_pre, short_pre = calc_long_short_prec(pred.iloc[:, 0], raw_label.iloc[:, 0], is_alpha=True)\n        ic, ric = calc_ic(pred.iloc[:, 0], raw_label.iloc[:, 0])","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/workflow/record_temp.py#L227-L263","documentation":"This NotImplementedError comes from the template-method pattern in qlib's record classes: generate() checks depend_cls data, then calls self._generate(*args, **kwargs) expecting the concrete subclass to return a dict of artifacts, which generate() then saves to the recorder. The base _generate raises by design, so the error means a record subclass implemented generate's contract partially — it inherited (or reused) generate but never provided _generate.","triggerScenarios":"Subclassing a record that inherits the shared generate() (the pattern used by HFSignalRecord-style classes) without overriding _generate; calling generate() on such a subclass; refactoring a record class and removing _generate while keeping the inherited generate.","commonSituations":"Creating custom signal-analysis records modeled on HFSignalRecord and forgetting the worker method; copy-paste of a record class where only the docstring was changed; assuming generate is the only hook to override.","solutions":["Implement _generate(self, *args, **kwargs) -> Dict[str, object] in the subclass, returning the artifacts dict that generate() will save via self.save(**artifact_dict)","Alternatively override generate() entirely in your subclass if the depend-check/save scaffolding does not fit, so _generate is never reached","Keep the return type a dict mapping artifact names to objects; non-dict returns skip saving but the NotImplementedError will already have fired before that matters"],"exampleFix":"# before\nclass MySigRecord(HFSignalRecord):\n    artifact_path = 'my_sig'\n    # _generate missing -> generate() hits base _generate\n\n# after\nclass MySigRecord(HFSignalRecord):\n    artifact_path = 'my_sig'\n    def _generate(self, *args, **kwargs) -> Dict[str, object]:\n        pred = self.load('pred.pkl')\n        return {'my_analysis.pkl': analyze(pred)}","handlingStrategy":"type-guard","validationCode":"from qlib.workflow.record_temp import RecordTemp\n\ndef assert_has_generate_impl(record):\n    if type(record)._generate is RecordTemp._generate:\n        raise TypeError(f'{type(record).__name__} must implement _generate() to reuse the shared generate()')","typeGuard":"from qlib.workflow.record_temp import RecordTemp\n\ndef has_generate_worker(record) -> bool:\n    return type(record)._generate is not RecordTemp._generate","tryCatchPattern":"try:\n    record.generate()\nexcept NotImplementedError as e:\n    if '_generate' in str(e):\n        raise TypeError(f'{type(record).__name__} forgot _generate; implement it or override generate()') from e\n    raise","preventionTips":["When inheriting a shared generate(), always implement _generate returning a dict","Model custom records on HFSignalRecord to get the dependency-check scaffolding","Unit-test each record subclass's generate end-to-end with a real recorder"],"tags":["qlib","record-temp","abstract-method","not-implemented"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}