{"record":{"id":"47be01eebfc99a4c","repo":"microsoft/qlib","slug":"please-implement-the-eq-method","errorCode":null,"errorMessage":"Please implement the `__eq__` method","messagePattern":"Please implement the `__eq__` method","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/backtest/high_performance_ds.py","lineNumber":249,"sourceCode":"        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\")\n\n    def count(self) -> int:\n        \"\"\"Return the count of the single metric, NaN is not included.\"\"\"","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/backtest/high_performance_ds.py#L231-L267","documentation":"BaseSingleMetric.__eq__ is declared abstract. Unlike normal Python objects, equality here is meant to return a new metric of element-wise boolean comparisons (return annotation is BaseSingleMetric), not a plain bool. The base class has no implementation, so == on a raw BaseSingleMetric raises NotImplementedError instead of comparing. SingleMetric implements it.","triggerScenarios":"m == 0.0 or m1 == m2 where m is a bare BaseSingleMetric; filtering code that builds boolean masks from metric comparisons; using BaseSingleMetric instances in set/dict membership tests (which call __eq__ via __hash__ machinery); incomplete subclasses that override comparison operators partially.","commonSituations":"Signal-masking code (value == target_price); tests asserting metric equality; custom boolean masks in strategy logic. Note: because __eq__ is overridden in subclasses, __hash__ behavior may change — avoid hashing metrics.","solutions":["Compare SingleMetric instances, which return element-wise metric results","Implement __eq__ in your subclass returning a metric of booleans (and keep the __hash__/__eq__ contract in mind)","For equality checks in tests, compare .storage (the underlying Series/dict) or use .sum()/.count() aggregations"],"exampleFix":"# before\nmask = BaseSingleMetric(s) == 0.0\n# after\nfrom qlib.backtest.high_performance_ds import SingleMetric\nmask = SingleMetric(s) == 0.0  # returns SingleMetric of booleans","handlingStrategy":"type-guard","validationCode":"from qlib.backtest.high_performance_ds import BaseSingleMetric\nassert type(m).__eq__ is not BaseSingleMetric.__eq__, \"object does not implement __eq__\"","typeGuard":"def supports_eq(m) -> bool:\n    from qlib.backtest.high_performance_ds import BaseSingleMetric\n    return isinstance(m, BaseSingleMetric) and type(m).__eq__ is not BaseSingleMetric.__eq__","tryCatchPattern":null,"preventionTips":["Remember metric __eq__ returns a boolean metric, not bool — never use it in `if m == x:`","Do not put metric objects in sets/dicts; key on stock ids instead","Compare underlying storage in tests: assert m.storage.equals(other.storage)"],"tags":["python","qlib","operator-overloading","equality","abstract-method","metrics"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}