Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/undate/undate.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def __contains__(self, other: object) -> bool:

@classmethod
def to_undate(cls, other: object) -> "Undate":
"""Converted arbitrary object to Undate, if possible. Raises TypeError
"""Convert arbitrary object to Undate, if possible. Raises TypeError
if conversion is not possible.

Currently suppports:
Expand All @@ -377,6 +377,9 @@ def to_undate(cls, other: object) -> "Undate":
return other
case datetime.date() | datetime.datetime():
return Undate(other.year, other.month, other.day)
case Date():
# handle conversion from internal Date class
return Undate(other.year, other.month, other.day)

case _:
raise TypeError(f"Conversion from {type(other)} is not supported")
Expand Down
10 changes: 9 additions & 1 deletion tests/test_undate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from undate import Undate, UndateInterval, Calendar
from undate.converters.base import BaseCalendarConverter
from undate.date import DatePrecision, Timedelta
from undate.date import Date, DatePrecision, Timedelta


class TestUndate:
Expand Down Expand Up @@ -152,6 +152,14 @@ def test_to_undate(self):
assert isinstance(undate_from_dt, Undate)
assert undate_from_dt == Undate(now.year, now.month, now.day)

# from internal Date object
y2k = Date(2000)
y2k_to_undate = Undate.to_undate(y2k)
assert isinstance(y2k_to_undate, Undate)
assert int(y2k_to_undate.year) == y2k.year
assert y2k_to_undate.month is None
assert y2k_to_undate.day is None

# unsupported type
with pytest.raises(TypeError):
Undate.to_undate("foo")
Expand Down