Skip to content

Commit

Permalink
Raise on __eq__ binary operations between unsupported types.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdice committed Aug 26, 2022
1 parent ccd72f2 commit ef0f676
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 20 additions & 2 deletions python/cudf/cudf/core/mixins/binops.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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(

Check warning on line 63 in python/cudf/cudf/core/mixins/binops.py

View check run for this annotation

Codecov / codecov/patch

python/cudf/cudf/core/mixins/binops.py#L62-L63

Added lines #L62 - L63 were not covered by tests
"'==' not supported between instances of "
f"'{type(self).__name__}' and '{type(other).__name__}'"
)
if op == "__ne__":
raise TypeError(

Check warning on line 68 in python/cudf/cudf/core/mixins/binops.py

View check run for this annotation

Codecov / codecov/patch

python/cudf/cudf/core/mixins/binops.py#L67-L68

Added lines #L67 - L68 were not covered by tests
"'!=' not supported between instances of "
f"'{type(self).__name__}' and '{type(other).__name__}'"
)
raise NotImplementedError()

Check warning on line 72 in python/cudf/cudf/core/mixins/binops.py

View check run for this annotation

Codecov / codecov/patch

python/cudf/cudf/core/mixins/binops.py#L72

Added line #L72 was not covered by tests


def _check_reflected_op(op):
Expand All @@ -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)
5 changes: 2 additions & 3 deletions python/cudf/cudf/core/mixins/mixin_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ef0f676

Please sign in to comment.