Skip to content

Commit ad5a344

Browse files
committed
Fix bugs with UserFloat
1 parent af78eac commit ad5a344

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

domdf_python_tools/bases.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def __index__(self) -> int:
367367

368368

369369
@prettify_docstrings
370-
class UserFloat(Real, SupportsRound):
370+
class UserFloat(Real):
371371
"""
372372
Class that simulates a float.
373373
@@ -450,37 +450,37 @@ def __round__(self, ndigits: Optional[int] = None) -> Union[int, float]: # type
450450
return float(self).__round__(ndigits)
451451

452452
def __eq__(self, other: object) -> bool:
453-
if isinstance(other, UserFloat):
453+
if isinstance(other, UserFloat) and not isinstance(other, float):
454454
return self._value == other._value
455455
else:
456456
return float(self).__eq__(other)
457457

458458
def __ne__(self, other: object) -> bool:
459-
if isinstance(other, UserFloat):
459+
if isinstance(other, UserFloat) and not isinstance(other, float):
460460
return self._value != other._value
461461
else:
462462
return float(self).__ne__(other)
463463

464464
def __lt__(self, other: Union[float, "UserFloat"]) -> bool:
465-
if isinstance(other, UserFloat):
465+
if isinstance(other, UserFloat) and not isinstance(other, float):
466466
return self._value < other._value
467467
else:
468468
return float(self).__lt__(other)
469469

470470
def __le__(self, other: Union[float, "UserFloat"]) -> bool:
471-
if isinstance(other, UserFloat):
471+
if isinstance(other, UserFloat) and not isinstance(other, float):
472472
return self._value <= other._value
473473
else:
474474
return float(self).__le__(other)
475475

476476
def __gt__(self, other: Union[float, "UserFloat"]) -> bool:
477-
if isinstance(other, UserFloat):
477+
if isinstance(other, UserFloat) and not isinstance(other, float):
478478
return self._value > other._value
479479
else:
480480
return float(self).__gt__(other)
481481

482482
def __ge__(self, other: Union[float, "UserFloat"]) -> bool:
483-
if isinstance(other, UserFloat):
483+
if isinstance(other, UserFloat) and not isinstance(other, float):
484484
return self._value >= other._value
485485
else:
486486
return float(self).__ge__(other)

tests/test_bases.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from numbers import Number, Real
1313

1414
# 3rd party
15+
from typing import no_type_check
16+
1517
import pytest
1618

1719
# this package
@@ -203,6 +205,7 @@ def test_rpow(self):
203205
assert 3**seven == 2187.0
204206
assert 3**seven == 2187
205207

208+
@no_type_check
206209
def test_round(self):
207210
assert isinstance(round(seven), int)
208211
assert round(seven) == 7
@@ -274,3 +277,8 @@ def test_hash(self):
274277
assert hash(seven) != hash(UserFloat(8))
275278
assert hash(seven) == hash(7)
276279
assert hash(seven) != hash(8)
280+
281+
def test_isinstance(self):
282+
assert isinstance(seven, UserFloat)
283+
assert not isinstance(seven, float)
284+
assert not isinstance(7, UserFloat)

0 commit comments

Comments
 (0)