From ef0f67662cdb8e18a0c45061f5e4d798ac265f5a Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Fri, 26 Aug 2022 05:47:39 -0500 Subject: [PATCH] Raise on __eq__ binary operations between unsupported types. --- python/cudf/cudf/core/mixins/binops.py | 22 +++++++++++++++++-- python/cudf/cudf/core/mixins/mixin_factory.py | 5 ++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/python/cudf/cudf/core/mixins/binops.py b/python/cudf/cudf/core/mixins/binops.py index eaabc00f266..282007135d7 100644 --- a/python/cudf/cudf/core/mixins/binops.py +++ b/python/cudf/cudf/core/mixins/binops.py @@ -1,6 +1,6 @@ # Copyright (c) 2022, NVIDIA CORPORATION. -from .mixin_factory import _create_delegating_mixin +from .mixin_factory import Operation, _create_delegating_mixin BinaryOperand = _create_delegating_mixin( "BinaryOperand", @@ -59,7 +59,17 @@ def _binaryop(self, other, op: str): Must be overridden by subclasses, the default implementation raises a NotImplementedError. """ - raise NotImplementedError + if op == "__eq__": + raise TypeError( + "'==' not supported between instances of " + f"'{type(self).__name__}' and '{type(other).__name__}'" + ) + if op == "__ne__": + raise TypeError( + "'!=' not supported between instances of " + f"'{type(self).__name__}' and '{type(other).__name__}'" + ) + raise NotImplementedError() def _check_reflected_op(op): @@ -70,3 +80,11 @@ def _check_reflected_op(op): BinaryOperand._binaryop = _binaryop BinaryOperand._check_reflected_op = staticmethod(_check_reflected_op) + +# It is necessary to override the default object.__eq__ so that objects don't +# automatically support equality binops using the wrong operator implementation +# (falling back to ``object``). We must override the object.__eq__ with an +# Operation rather than a plain function/method, so that the BinaryOperand +# mixin overrides it for classes that define their own __eq__ in _binaryop. +BinaryOperand.__eq__ = Operation("__eq__", {}, BinaryOperand._binaryop) +BinaryOperand.__ne__ = Operation("__ne__", {}, BinaryOperand._binaryop) diff --git a/python/cudf/cudf/core/mixins/mixin_factory.py b/python/cudf/cudf/core/mixins/mixin_factory.py index 7bbb299d643..e3564bf4dc6 100644 --- a/python/cudf/cudf/core/mixins/mixin_factory.py +++ b/python/cudf/cudf/core/mixins/mixin_factory.py @@ -27,9 +27,8 @@ class Operation: ---------- name : str The name of the operation. - docstring_format_args : str - The attribute of the owning class from which to pull format parameters - for this operation's docstring. + docstring_format_args : dict + The dict of format parameters for this operation's docstring. base_operation : str The underlying operation function to be invoked when operation `name` is called on the owning class.