Skip to content

Commit a4f2e7b

Browse files
committed
Add more type checks and tests
1 parent fc4f7a9 commit a4f2e7b

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

src/undate/interval.py

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def __repr__(self) -> str:
7373
return "<UndateInterval %s>" % self
7474

7575
def __eq__(self, other) -> bool:
76+
# currently doesn't support comparison with any other types
77+
if not isinstance(other, UndateInterval):
78+
return NotImplemented
7679
# consider interval equal if both dates are equal
7780
return self.earliest == other.earliest and self.latest == other.latest
7881

tests/test_interval.py

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ def test_eq(self):
8282
)
8383
assert UndateInterval(Undate(2022, 5)) == UndateInterval(Undate(2022, 5))
8484

85+
def test_eq_type_check(self):
86+
# doesn't currently support comparison with anything else
87+
interval = UndateInterval(Undate(900))
88+
# returns NotIplemented if comparison with this type is not supported
89+
assert interval.__eq__("foo") == NotImplemented
90+
8591
def test_not_eq(self):
8692
assert UndateInterval(Undate(2022), Undate(2023)) != UndateInterval(
8793
Undate(2022), Undate(2024)

tests/test_undate.py

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ def test_to_undate(self):
152152
assert isinstance(undate_from_dt, Undate)
153153
assert undate_from_dt == Undate(now.year, now.month, now.day)
154154

155+
# unsupported type
156+
with pytest.raises(TypeError):
157+
Undate.to_undate("foo")
158+
155159
# test properties for accessing parts of date
156160
def test_year_property(self):
157161
# two, three, four five digit years; numeric and string

0 commit comments

Comments
 (0)