Skip to content

Commit 2d2080a

Browse files
committed
Object comparison with something else should fail gracefully
Seems that __eq__ should not raise NotImplementederror python/mypy#2783 (comment)
1 parent 6bd2cf8 commit 2d2080a

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

rotkehlchen/assets/asset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ def __eq__(self, other: Any) -> bool:
197197
return self.identifier.lower() == other.identifier.lower()
198198
if isinstance(other, str):
199199
return self.identifier.lower() == other.lower()
200-
# else
201-
raise NotImplementedError(f'Invalid comparison of asset with {type(other)}')
200+
201+
return False
202202

203203
def __lt__(self, other: Union['Asset', str]) -> bool:
204204
if isinstance(other, Asset):

rotkehlchen/fval.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,75 +52,82 @@ def __repr__(self) -> str:
5252
return 'FVal({})'.format(str(self.num))
5353

5454
def __gt__(self, other: AcceptableFValOtherInput) -> bool:
55-
evaluated_other = evaluate_input(other)
55+
evaluated_other = _evaluate_input(other)
5656
return self.num.compare_signal(evaluated_other) == Decimal('1')
5757

5858
def __lt__(self, other: AcceptableFValOtherInput) -> bool:
59-
evaluated_other = evaluate_input(other)
59+
evaluated_other = _evaluate_input(other)
6060
return self.num.compare_signal(evaluated_other) == Decimal('-1')
6161

6262
def __le__(self, other: AcceptableFValOtherInput) -> bool:
63-
evaluated_other = evaluate_input(other)
63+
evaluated_other = _evaluate_input(other)
6464
return self.num.compare_signal(evaluated_other) in (Decimal('-1'), Decimal('0'))
6565

6666
def __ge__(self, other: AcceptableFValOtherInput) -> bool:
67-
evaluated_other = evaluate_input(other)
67+
evaluated_other = _evaluate_input(other)
6868
return self.num.compare_signal(evaluated_other) in (Decimal('1'), Decimal('0'))
6969

7070
def __eq__(self, other: object) -> bool:
71-
evaluated_other = evaluate_input(other)
71+
evaluated_other: Union[Decimal, int]
72+
if isinstance(other, FVal):
73+
evaluated_other = other.num
74+
elif not isinstance(other, int):
75+
return False
76+
else:
77+
evaluated_other = other
78+
7279
return self.num.compare_signal(evaluated_other) == Decimal('0')
7380

7481
def __add__(self, other: AcceptableFValOtherInput) -> 'FVal':
75-
evaluated_other = evaluate_input(other)
82+
evaluated_other = _evaluate_input(other)
7683
return FVal(self.num.__add__(evaluated_other))
7784

7885
def __sub__(self, other: AcceptableFValOtherInput) -> 'FVal':
79-
evaluated_other = evaluate_input(other)
86+
evaluated_other = _evaluate_input(other)
8087
return FVal(self.num.__sub__(evaluated_other))
8188

8289
def __mul__(self, other: AcceptableFValOtherInput) -> 'FVal':
83-
evaluated_other = evaluate_input(other)
90+
evaluated_other = _evaluate_input(other)
8491
return FVal(self.num.__mul__(evaluated_other))
8592

8693
def __truediv__(self, other: AcceptableFValOtherInput) -> 'FVal':
87-
evaluated_other = evaluate_input(other)
94+
evaluated_other = _evaluate_input(other)
8895
return FVal(self.num.__truediv__(evaluated_other))
8996

9097
def __floordiv__(self, other: AcceptableFValOtherInput) -> 'FVal':
91-
evaluated_other = evaluate_input(other)
98+
evaluated_other = _evaluate_input(other)
9299
return FVal(self.num.__floordiv__(evaluated_other))
93100

94101
def __pow__(self, other: AcceptableFValOtherInput) -> 'FVal':
95-
evaluated_other = evaluate_input(other)
102+
evaluated_other = _evaluate_input(other)
96103
return FVal(self.num.__pow__(evaluated_other))
97104

98105
def __radd__(self, other: AcceptableFValOtherInput) -> 'FVal':
99-
evaluated_other = evaluate_input(other)
106+
evaluated_other = _evaluate_input(other)
100107
return FVal(self.num.__radd__(evaluated_other))
101108

102109
def __rsub__(self, other: AcceptableFValOtherInput) -> 'FVal':
103-
evaluated_other = evaluate_input(other)
110+
evaluated_other = _evaluate_input(other)
104111
return FVal(self.num.__rsub__(evaluated_other))
105112

106113
def __rmul__(self, other: AcceptableFValOtherInput) -> 'FVal':
107-
evaluated_other = evaluate_input(other)
114+
evaluated_other = _evaluate_input(other)
108115
return FVal(self.num.__rmul__(evaluated_other))
109116

110117
def __rtruediv__(self, other: AcceptableFValOtherInput) -> 'FVal':
111-
evaluated_other = evaluate_input(other)
118+
evaluated_other = _evaluate_input(other)
112119
return FVal(self.num.__rtruediv__(evaluated_other))
113120

114121
def __rfloordiv__(self, other: AcceptableFValOtherInput) -> 'FVal':
115-
evaluated_other = evaluate_input(other)
122+
evaluated_other = _evaluate_input(other)
116123
return FVal(self.num.__rfloordiv__(evaluated_other))
117124

118125
def __mod__(self, other: AcceptableFValOtherInput) -> 'FVal':
119-
evaluated_other = evaluate_input(other)
126+
evaluated_other = _evaluate_input(other)
120127
return FVal(self.num.__mod__(evaluated_other))
121128

122129
def __rmod__(self, other: AcceptableFValOtherInput) -> 'FVal':
123-
evaluated_other = evaluate_input(other)
130+
evaluated_other = _evaluate_input(other)
124131
return FVal(self.num.__rmod__(evaluated_other))
125132

126133
def __float__(self) -> float:
@@ -141,8 +148,8 @@ def fma(self, other: AcceptableFValOtherInput, third: AcceptableFValOtherInput)
141148
Fused multiply-add. Return self*other+third with no rounding of the
142149
intermediate product self*other
143150
"""
144-
evaluated_other = evaluate_input(other)
145-
evaluated_third = evaluate_input(third)
151+
evaluated_other = _evaluate_input(other)
152+
evaluated_third = _evaluate_input(third)
146153
return FVal(self.num.fma(evaluated_other, evaluated_third))
147154

148155
def to_percentage(self, precision: int = 4, with_perc_sign: bool = True) -> str:
@@ -170,7 +177,7 @@ def is_close(self, other: AcceptableFValInitInput, max_diff: str = "1e-6") -> bo
170177
return diff_num <= evaluated_max_diff.num
171178

172179

173-
def evaluate_input(other: Any) -> Union[Decimal, int]:
180+
def _evaluate_input(other: Any) -> Union[Decimal, int]:
174181
"""Evaluate 'other' and return its Decimal representation"""
175182
if isinstance(other, FVal):
176183
return other.num

0 commit comments

Comments
 (0)