Skip to content

Commit 58e2448

Browse files
committed
Fix undelta repr so it works with eval()
1 parent 51061c4 commit 58e2448

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/undate/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
__version__ = "0.6.0.dev0"
22

3-
from undate.date import DatePrecision
3+
from undate.date import DatePrecision, UnDelta
44
from undate.undate import Undate, Calendar
55
from undate.interval import UndateInterval
66

7-
__all__ = ["Undate", "UndateInterval", "Calendar", "DatePrecision", "__version__"]
7+
__all__ = [
8+
"Undate",
9+
"UndateInterval",
10+
"Calendar",
11+
"DatePrecision",
12+
"UnDelta",
13+
"__version__",
14+
]

src/undate/date.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def __init__(self, *days: int):
145145
def __repr__(self):
146146
# customize string representation for simpler notation; default
147147
# specifies full UnInt initialization with upper and lower keywords
148-
return f"{self.__class__.__name__}(days=[{self.days.lower},{self.days.upper}])"
148+
return f"undate.{self.__class__.__name__}({self.days.lower},{self.days.upper})"
149149

150150
def __eq__(self, other: object) -> bool:
151151
# is an uncertain duration ever *equal* another, even if the values are the same?

tests/test_date.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,20 @@ def test_init_validation(self):
230230
UnDelta(10)
231231

232232
def test_repr(self):
233-
# customized string representation
234-
assert repr(UnDelta(28, 29)) == "UnDelta(days=[28,29])"
233+
# test customized string representation
234+
235+
# import undate to test eval of fully-qualified repr string
236+
import undate # noqa: F401
237+
238+
feb_undelt = UnDelta(28, 29)
239+
assert repr(feb_undelt) == "undate.UnDelta(28,29)"
240+
# can't compare directly because uncertain deltas aren't equal,
241+
# but compare vlaues
242+
assert eval(repr(feb_undelt.days.lower)) == feb_undelt.days.lower
243+
assert eval(repr(feb_undelt.days.upper)) == feb_undelt.days.upper
244+
245+
larger_undelt = UnDelta(10, 12, 14, 16)
246+
assert repr(larger_undelt) == "undate.UnDelta(10,16)"
235247

236248
def test_eq(self):
237249
# uncertain deltas are not equivalent

0 commit comments

Comments
 (0)