{"record":{"id":"3afbeb43023befe5","repo":"microsoft/qlib","slug":"please-implement-the-gt-method","errorCode":null,"errorMessage":"Please implement the `__gt__` method","messagePattern":"Please implement the `__gt__` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":252,"sourceCode":"        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\")\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","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L234-L270","documentation":"BaseSingleMetric.__gt__ is the abstract greater-than operator, intended to return an element-wise boolean metric (return type BaseSingleMetric), not a bool. The base class raises NotImplementedError; SingleMetric provides the implementation. Applying > to a bare BaseSingleMetric raises this error.","triggerScenarios":"m > threshold or m1 > m2 on a raw BaseSingleMetric; strategy filter code like (momentum > 0) to build tradable universes; sorted()/max() calls that invoke __gt__ on metric objects; subclasses implementing __lt__ but not __gt__.","commonSituations":"Thresholding factor scores into buy/sell masks; ranking code that accidentally sorts metric objects instead of their underlying Series; partial operator coverage in user subclasses.","solutions":["Use SingleMetric for comparisons; convert the result with .astype or use its storage when a pandas boolean Series is needed","Implement __gt__ in your subclass returning type(self)(self.storage > other)","Sort/compare the underlying .storage Series rather than metric objects"],"exampleFix":"# before\nbuy = BaseSingleMetric(score) > 0.0\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nbuy = SingleMetric(score) > 0.0  # SingleMetric of booleans","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).__gt__ is not BaseSingleMetric.__gt__, \"object does not implement __gt__\"","typeGuard":"def supports_gt(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).__gt__ is not BaseSingleMetric.__gt__","tryCatchPattern":null,"preventionTips":["Use SingleMetric when thresholding into boolean masks","Never sort metric objects; sort m.storage instead","Implement __gt__ and __lt__ together in subclasses"],"tags":["python","qlib","operator-overloading","comparison","abstract-method","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}