gh-107773: Make `datetime` subclass `repr` consistent both implementa… · python/cpython@81a9b53 · GitHub
Skip to content

Commit 81a9b53

Browse files
donbarbosjaraco
andauthored
gh-107773: Make datetime subclass repr consistent both implementations (#130308)
--------- Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
1 parent 9f81f82 commit 81a9b53

3 files changed

Lines changed: 68 additions & 21 deletions

File tree

Lib/_pydatetime.py

Lines changed: 23 additions & 21 deletions

Lib/test/datetimetester.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ def test_harmful_mixed_comparison(self):
504504
#############################################################################
505505
# timedelta tests
506506

507+
class SubclassTimeDelta(timedelta):
508+
sub_var = 1
509+
507510
class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
508511

509512
theclass = timedelta
@@ -788,6 +791,15 @@ def test_repr(self):
788791
self.assertEqual(repr(self.theclass(seconds=1, microseconds=100)),
789792
"%s(seconds=1, microseconds=100)" % name)
790793

794+
def test_repr_subclass(self):
795+
"""Subclasses should have bare names in the repr (gh-107773)."""
796+
td = SubclassTimeDelta(days=1)
797+
self.assertEqual(repr(td), "SubclassTimeDelta(days=1)")
798+
td = SubclassTimeDelta(seconds=30)
799+
self.assertEqual(repr(td), "SubclassTimeDelta(seconds=30)")
800+
td = SubclassTimeDelta(weeks=2)
801+
self.assertEqual(repr(td), "SubclassTimeDelta(days=14)")
802+
791803
def test_roundtrip(self):
792804
for td in (timedelta(days=999999999, hours=23, minutes=59,
793805
seconds=59, microseconds=999999),
@@ -1224,6 +1236,15 @@ def test_roundtrip(self):
12241236
dt2 = self.theclass(dt.year, dt.month, dt.day)
12251237
self.assertEqual(dt, dt2)
12261238

1239+
def test_repr_subclass(self):
1240+
"""Subclasses should have bare names in the repr (gh-107773)."""
1241+
td = SubclassDate(1, 2, 3)
1242+
self.assertEqual(repr(td), "SubclassDate(1, 2, 3)")
1243+
td = SubclassDate(2014, 1, 1)
1244+
self.assertEqual(repr(td), "SubclassDate(2014, 1, 1)")
1245+
td = SubclassDate(2010, 10, day=10)
1246+
self.assertEqual(repr(td), "SubclassDate(2010, 10, 10)")
1247+
12271248
def test_ordinal_conversions(self):
12281249
# Check some fixed values.
12291250
for y, m, d, n in [(1, 1, 1, 1), # calendar origin
@@ -3587,6 +3608,15 @@ class DateTimeSubclass(self.theclass):
35873608
self.assertEqual(dt, dt_rt)
35883609
self.assertIsInstance(dt_rt, DateTimeSubclass)
35893610

3611+
def test_repr_subclass(self):
3612+
"""Subclasses should have bare names in the repr (gh-107773)."""
3613+
td = SubclassDatetime(2014, 1, 1)
3614+
self.assertEqual(repr(td), "SubclassDatetime(2014, 1, 1, 0, 0)")
3615+
td = SubclassDatetime(2010, 10, day=10)
3616+
self.assertEqual(repr(td), "SubclassDatetime(2010, 10, 10, 0, 0)")
3617+
td = SubclassDatetime(2010, 10, 2, second=3)
3618+
self.assertEqual(repr(td), "SubclassDatetime(2010, 10, 2, 0, 0, 3)")
3619+
35903620

35913621
class TestSubclassDateTime(TestDateTime):
35923622
theclass = SubclassDatetime
@@ -3897,6 +3927,19 @@ def test_repr(self):
38973927
self.assertEqual(repr(self.theclass(23, 15, 0, 0)),
38983928
"%s(23, 15)" % name)
38993929

3930+
def test_repr_subclass(self):
3931+
"""Subclasses should have bare names in the repr (gh-107773)."""
3932+
td = SubclassTime(hour=1)
3933+
self.assertEqual(repr(td), "SubclassTime(1, 0)")
3934+
td = SubclassTime(hour=2, minute=30)
3935+
self.assertEqual(repr(td), "SubclassTime(2, 30)")
3936+
td = SubclassTime(hour=2, minute=30, second=11)
3937+
self.assertEqual(repr(td), "SubclassTime(2, 30, 11)")
3938+
td = SubclassTime(minute=30, second=11, fold=0)
3939+
self.assertEqual(repr(td), "SubclassTime(0, 30, 11)")
3940+
td = SubclassTime(minute=30, second=11, fold=1)
3941+
self.assertEqual(repr(td), "SubclassTime(0, 30, 11, fold=1)")
3942+
39003943
def test_resolution_info(self):
39013944
self.assertIsInstance(self.theclass.min, self.theclass)
39023945
self.assertIsInstance(self.theclass.max, self.theclass)
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)