Skip to content

Commit 20a9f8d

Browse files
committed
Fix comparison methods and add tests; incorporate @coderabbitai comments
1 parent 4e5b243 commit 20a9f8d

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/undate/date.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def __iter__(self) -> Iterable:
5757
def __gt__(self, other: object) -> bool:
5858
match other:
5959
case int():
60-
return self.upper > other
60+
return self.lower > other
6161
case UnInt():
62-
return self.upper > other.lower
62+
return self.lower > other.upper
6363
case _:
6464
return NotImplemented
6565

@@ -90,22 +90,22 @@ def _replace_with(self, other_lower, other_upper, op):
9090
self, lower=op(self.lower, other_lower), upper=op(self.upper, other_upper)
9191
)
9292

93-
def __add__(self, other: object) -> bool:
93+
def __add__(self, other: object) -> "UnInt":
9494
match other:
9595
case int():
9696
# increase both values by the added amount
9797
add_values = (other, other)
9898
case UnInt():
99-
# subtract the upper and lower values by the other lower and upper
100-
# to include the largest range of possible values
99+
# add other lower value to current lower and other upper
100+
# to current upper to include the largest range of possible values
101101
# (when calculating with uncertain values, the uncertainty increases)
102102
add_values = (other.lower, other.upper)
103103
case _:
104104
return NotImplemented
105105

106106
return self._replace_with(*add_values, operator.add)
107107

108-
def __sub__(self, other):
108+
def __sub__(self, other) -> "UnInt":
109109
match other:
110110
case int():
111111
# decrease both values by the subtracted amount
@@ -147,14 +147,12 @@ def __repr__(self):
147147
# specifies full UnInt initialization with upper and lower keywords
148148
return f"{self.__class__.__name__}(days=[{self.days.lower},{self.days.upper}])"
149149

150-
# TODO: what does equality for an uncertain range mean?
151-
# is an uncertain range ever equal to another uncertain range?
152-
153150
def __eq__(self, other: object) -> bool:
154151
# is an uncertain duration ever *equal* another, even if the values are the same?
155-
if other is self:
156-
return True
157-
return False
152+
# for now, make the assumption that we only want identity equality
153+
# and not value equality; perhaps in future we can revisit
154+
# or add functions to check value equality / equivalence / similarity
155+
return other is self
158156

159157
def __lt__(self, other: object) -> bool:
160158
match other:

tests/test_date.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,34 @@ def test_contains(self):
120120
# other types are assumed not in range
121121
assert "twenty-eight" not in anymonth_days
122122

123+
def test_gt(self):
124+
ten_twelve = UnInt(10, 12)
125+
# compare with integer
126+
assert 13 > ten_twelve
127+
assert not 12 > ten_twelve
128+
assert not 9 > ten_twelve
129+
# compare with unint
130+
assert UnInt(13, 23) > ten_twelve
131+
assert not UnInt(12, 24) > ten_twelve
132+
assert not UnInt(2, 4) > ten_twelve
133+
# unsupported type
134+
with pytest.raises(TypeError):
135+
ten_twelve > "three"
136+
137+
def test_lt(self):
138+
ten_twelve = UnInt(10, 12)
139+
# compare with integer
140+
assert 9 < ten_twelve
141+
assert not 12 < ten_twelve
142+
assert not 13 < ten_twelve
143+
# compare with unint
144+
assert UnInt(2, 4) < ten_twelve
145+
assert not UnInt(12, 24) < ten_twelve
146+
assert not UnInt(13, 23) < ten_twelve
147+
# unsupported type
148+
with pytest.raises(TypeError):
149+
ten_twelve < "three"
150+
123151
def test_iterable(self):
124152
anymonth_days = UnInt(lower=28, upper=31)
125153
assert list(anymonth_days) == [28, 29, 30, 31]

0 commit comments

Comments
 (0)