Skip to content

Commit fb694b7

Browse files
committed
Support conversion from internal Date class to Undate #119
1 parent 8a01277 commit fb694b7

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

src/undate/undate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def __contains__(self, other: object) -> bool:
365365

366366
@classmethod
367367
def to_undate(cls, other: object) -> "Undate":
368-
"""Converted arbitrary object to Undate, if possible. Raises TypeError
368+
"""Convert arbitrary object to Undate, if possible. Raises TypeError
369369
if conversion is not possible.
370370
371371
Currently suppports:
@@ -377,6 +377,9 @@ def to_undate(cls, other: object) -> "Undate":
377377
return other
378378
case datetime.date() | datetime.datetime():
379379
return Undate(other.year, other.month, other.day)
380+
case Date():
381+
# handle conversion from internal Date class
382+
return Undate(other.year, other.month, other.day)
380383

381384
case _:
382385
raise TypeError(f"Conversion from {type(other)} is not supported")

tests/test_undate.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from undate import Undate, UndateInterval, Calendar
66
from undate.converters.base import BaseCalendarConverter
7-
from undate.date import DatePrecision, Timedelta
7+
from undate.date import Date, DatePrecision, Timedelta
88

99

1010
class TestUndate:
@@ -152,6 +152,14 @@ 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+
# from internal Date object
156+
y2k = Date(2000)
157+
y2k_to_undate = Undate.to_undate(y2k)
158+
assert isinstance(y2k_to_undate, Undate)
159+
assert int(y2k_to_undate.year) == y2k.year
160+
assert y2k_to_undate.month is None
161+
assert y2k_to_undate.day is None
162+
155163
# unsupported type
156164
with pytest.raises(TypeError):
157165
Undate.to_undate("foo")

0 commit comments

Comments
 (0)