Raise Exception in Python when attempting to operate on incompatible types, as this will be invalid in JS
This commit is contained in:
parent
f0e8bac4b7
commit
803941a53a
@ -44,23 +44,41 @@ class Fixed:
|
|||||||
return Rational(self.impl)
|
return Rational(self.impl)
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Fixed(self.impl + other.impl)
|
return Fixed(self.impl + other.impl)
|
||||||
def __sub__(self, other):
|
def __sub__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Fixed(self.impl - other.impl)
|
return Fixed(self.impl - other.impl)
|
||||||
def __mul__(self, other):
|
def __mul__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Fixed(self.impl * other.impl)
|
return Fixed(self.impl * other.impl)
|
||||||
def __truediv__(self, other):
|
def __truediv__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Fixed(self.impl / other.impl)
|
return Fixed(self.impl / other.impl)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl == other.impl
|
return self.impl == other.impl
|
||||||
def __gt__(self, other):
|
def __gt__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl > other.impl
|
return self.impl > other.impl
|
||||||
def __ge__(self, other):
|
def __ge__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl >= other.impl
|
return self.impl >= other.impl
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl < other.impl
|
return self.impl < other.impl
|
||||||
def __le__(self, other):
|
def __le__(self, other):
|
||||||
|
if not isinstance(other, Fixed):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl <= other.impl
|
return self.impl <= other.impl
|
||||||
|
|
||||||
def __floor__(self):
|
def __floor__(self):
|
||||||
|
@ -37,23 +37,41 @@ class NativeInt:
|
|||||||
return Rational(self.impl)
|
return Rational(self.impl)
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return NativeInt(self.impl + other.impl)
|
return NativeInt(self.impl + other.impl)
|
||||||
def __sub__(self, other):
|
def __sub__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return NativeInt(self.impl - other.impl)
|
return NativeInt(self.impl - other.impl)
|
||||||
def __mul__(self, other):
|
def __mul__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return NativeInt(self.impl * other.impl)
|
return NativeInt(self.impl * other.impl)
|
||||||
def __truediv__(self, other):
|
def __truediv__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return NativeInt(self.impl / other.impl)
|
return NativeInt(self.impl / other.impl)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl == other.impl
|
return self.impl == other.impl
|
||||||
def __gt__(self, other):
|
def __gt__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl > other.impl
|
return self.impl > other.impl
|
||||||
def __ge__(self, other):
|
def __ge__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl >= other.impl
|
return self.impl >= other.impl
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl < other.impl
|
return self.impl < other.impl
|
||||||
def __le__(self, other):
|
def __le__(self, other):
|
||||||
|
if not isinstance(other, NativeInt):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl <= other.impl
|
return self.impl <= other.impl
|
||||||
|
|
||||||
def __floor__(self):
|
def __floor__(self):
|
||||||
|
@ -37,23 +37,41 @@ class Native:
|
|||||||
return Rational(self.impl)
|
return Rational(self.impl)
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Native(self.impl + other.impl)
|
return Native(self.impl + other.impl)
|
||||||
def __sub__(self, other):
|
def __sub__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Native(self.impl - other.impl)
|
return Native(self.impl - other.impl)
|
||||||
def __mul__(self, other):
|
def __mul__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Native(self.impl * other.impl)
|
return Native(self.impl * other.impl)
|
||||||
def __truediv__(self, other):
|
def __truediv__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Native(self.impl / other.impl)
|
return Native(self.impl / other.impl)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl == other.impl
|
return self.impl == other.impl
|
||||||
def __gt__(self, other):
|
def __gt__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl > other.impl
|
return self.impl > other.impl
|
||||||
def __ge__(self, other):
|
def __ge__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl >= other.impl
|
return self.impl >= other.impl
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl < other.impl
|
return self.impl < other.impl
|
||||||
def __le__(self, other):
|
def __le__(self, other):
|
||||||
|
if not isinstance(other, Native):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl <= other.impl
|
return self.impl <= other.impl
|
||||||
|
|
||||||
def __floor__(self):
|
def __floor__(self):
|
||||||
|
@ -46,23 +46,41 @@ class Rational:
|
|||||||
return Num(self.impl.numerator) / Num(self.impl.denominator)
|
return Num(self.impl.numerator) / Num(self.impl.denominator)
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Rational(self.impl + other.impl)
|
return Rational(self.impl + other.impl)
|
||||||
def __sub__(self, other):
|
def __sub__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Rational(self.impl - other.impl)
|
return Rational(self.impl - other.impl)
|
||||||
def __mul__(self, other):
|
def __mul__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Rational(self.impl * other.impl)
|
return Rational(self.impl * other.impl)
|
||||||
def __truediv__(self, other):
|
def __truediv__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return Rational(self.impl / other.impl)
|
return Rational(self.impl / other.impl)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl == other.impl
|
return self.impl == other.impl
|
||||||
def __gt__(self, other):
|
def __gt__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl > other.impl
|
return self.impl > other.impl
|
||||||
def __ge__(self, other):
|
def __ge__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl >= other.impl
|
return self.impl >= other.impl
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl < other.impl
|
return self.impl < other.impl
|
||||||
def __le__(self, other):
|
def __le__(self, other):
|
||||||
|
if not isinstance(other, Rational):
|
||||||
|
raise ValueError('Attempt to operate on incompatible types')
|
||||||
return self.impl <= other.impl
|
return self.impl <= other.impl
|
||||||
|
|
||||||
def __floor__(self):
|
def __floor__(self):
|
||||||
|
Reference in New Issue
Block a user