Skip to content

Commit 4fe0df1

Browse files
committed
fix: reflective dunder methods operation order
The reflective dunder methods are called when the normal dunder fails. This normally does not matter for commutative operations, like addition, but for division and subtraction, this can lead to errors. For example, def __sub__(self, other) is meant to solve `self - other` while def __rsub__(self, other) is meant to solve `other - self` The following reflective dunder methods were fixed to honor the correct direction of operations: __rsub__ __rfloordiv__ __rtruediv__ __rmod__ __rpow__
1 parent a3d1a48 commit 4fe0df1

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

NumpyDeque/NumpyDeque.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,12 @@ def __sub__(self, other): # To get called on subtraction operation using - oper
826826
return res
827827

828828
def __rsub__(self, other): # To get called on subtraction operation using - operator.
829-
res = self.copy()
830-
res -= other
829+
if isinstance(other, NumpyDeque):
830+
res = other.copy()
831+
res -= self.deque
832+
else:
833+
res = NumpyDeque.array(other)
834+
res -= self.deque
831835
return res
832836

833837
def __mul__(self, other): # To get called on multiplication operation using * operator.
@@ -846,8 +850,12 @@ def __floordiv__(self, other): # To get called on floor division operation usin
846850
return res
847851

848852
def __rfloordiv__(self, other): # To get called on floor division operation using // operator.
849-
res = self.copy()
850-
res //= other
853+
if isinstance(other, NumpyDeque):
854+
res = other.copy()
855+
res //= self.deque
856+
else:
857+
res = NumpyDeque.array(other)
858+
res //= self.deque
851859
return res
852860

853861
def __truediv__(self, other): # To get called on division operation using / operator.
@@ -856,8 +864,12 @@ def __truediv__(self, other): # To get called on division operation using / ope
856864
return res
857865

858866
def __rtruediv__(self, other): # To get called on division operation using / operator.
859-
res = self.copy()
860-
res /= other
867+
if isinstance(other, NumpyDeque):
868+
res = other.copy()
869+
res /= self.deque
870+
else:
871+
res = NumpyDeque.array(other)
872+
res /= self.deque
861873
return res
862874

863875
def __mod__(self, other): # To get called on modulo operation using % operator.
@@ -866,8 +878,12 @@ def __mod__(self, other): # To get called on modulo operation using % operator.
866878
return res
867879

868880
def __rmod__(self, other): # To get called on modulo operation using % operator.
869-
res = self.copy()
870-
res %= other
881+
if isinstance(other, NumpyDeque):
882+
res = other.copy()
883+
res %= self.deque
884+
else:
885+
res = NumpyDeque.array(other)
886+
res %= self.deque
871887
return res
872888

873889
def __pow__(self, other): # To get called on calculating the power using ** operator.
@@ -876,8 +892,12 @@ def __pow__(self, other): # To get called on calculating the power using ** ope
876892
return res
877893

878894
def __rpow__(self, other): # To get called on calculating the power using ** operator.
879-
res = self.copy()
880-
res **= other
895+
if isinstance(other, NumpyDeque):
896+
res = other.copy()
897+
res **= self.deque
898+
else:
899+
res = NumpyDeque.array(other)
900+
res **= self.deque
881901
return res
882902

883903
def __lt__(self, other): # To get called on comparison using < operator.

0 commit comments

Comments
 (0)