Skip to content

Commit

Permalink
Refine eq and contains checks for unknown and partial unknowns
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed Oct 27, 2023
1 parent 34b6688 commit 8cfa8f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
4 changes: 1 addition & 3 deletions src/undate/undate.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,9 @@ def __contains__(self, other: "Undate") -> bool:
return (
self.earliest <= other.earliest
and self.latest >= other.latest
# precision is not sufficient for comparing partially known dates
# is precision sufficient for comparing partially known dates?
and self.precision > other.precision
)
# TODO: how to compare partially unknown values
# like 19xx and 199x or 1801-XX and 1801-1X

@property
def known_year(self) -> bool:
Expand Down
27 changes: 11 additions & 16 deletions tests/test_undate.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ def test_lt_notimplemented(self):
(Undate(2022, 1, 1), Undate(2022)),
(Undate(2022, 12, 31), Undate(2022)),
(Undate(2022, 6, 15), Undate(2022, 6)),
# TODO: support partially known dates that are unambiguously in range
# (Undate("199X"), Undate("19XX")),
]

@pytest.mark.parametrize("date1,date2", testdata_contains)
Expand All @@ -227,26 +225,23 @@ def test_contains(self, date1, date2):
(Undate(1980), Undate(2020)),
(Undate(1980), Undate(2020, 6)),
(Undate(1980, 6), Undate(2020, 6)),
# partially known dates that are similar but same precision,
# so one does not contain the other
(Undate("199X"), Undate("19XX")),
# - specific month to unknown month
(Undate(1980, 6), Undate(1980, "XX")),
# some of these might overlap, but we don't have enough
# information to determine
# - unknown month to unknown month
(Undate(1980, "XX"), Undate(1980, "XX")),
# - partially unknown month to unknown month
(Undate(1801, "1X"), Undate(1801, "XX")),
]

@pytest.mark.parametrize("date1,date2", testdata_not_contains)
def test_not_contains(self, date1, date2):
assert date1 not in date2

def test_contains_ambiguous(self):
# date not in range due to precision
# TODO: can we return an unknown instead of false?
# or should this raise a not implemented error?

# these are cases where dates *might* overlap,
# but we don't have enough information to determine
# - specific month to unknown month
assert Undate(1980, 6) not in Undate(1980, "XX")
# - unknown month to unknown month
assert Undate(1980, "XX") not in Undate(1980, "XX")
assert Undate(1980, 6) not in Undate(1980, "XX")
assert Undate(1801, "1X") not in Undate(1801, "XX")

def test_sorting(self):
# sorting should be possible based on gt/lt
# test simple cases for sorting
Expand Down

0 comments on commit 8cfa8f0

Please sign in to comment.