bpo-30302 Make timedelta.__repr__ more informative. (#1493)
diff --git a/Lib/datetime.py b/Lib/datetime.py
index b95536f..76a6f95 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -454,20 +454,18 @@
return self
def __repr__(self):
- if self._microseconds:
- return "%s.%s(%d, %d, %d)" % (self.__class__.__module__,
- self.__class__.__qualname__,
- self._days,
- self._seconds,
- self._microseconds)
+ args = []
+ if self._days:
+ args.append("days=%d" % self._days)
if self._seconds:
- return "%s.%s(%d, %d)" % (self.__class__.__module__,
- self.__class__.__qualname__,
- self._days,
- self._seconds)
- return "%s.%s(%d)" % (self.__class__.__module__,
+ args.append("seconds=%d" % self._seconds)
+ if self._microseconds:
+ args.append("microseconds=%d" % self._microseconds)
+ if not args:
+ args.append('0')
+ return "%s.%s(%s)" % (self.__class__.__module__,
self.__class__.__qualname__,
- self._days)
+ ', '.join(args))
def __str__(self):
mm, ss = divmod(self._seconds, 60)
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index b25e6c1..2200888 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -658,11 +658,21 @@
def test_repr(self):
name = 'datetime.' + self.theclass.__name__
self.assertEqual(repr(self.theclass(1)),
- "%s(1)" % name)
+ "%s(days=1)" % name)
self.assertEqual(repr(self.theclass(10, 2)),
- "%s(10, 2)" % name)
+ "%s(days=10, seconds=2)" % name)
self.assertEqual(repr(self.theclass(-10, 2, 400000)),
- "%s(-10, 2, 400000)" % name)
+ "%s(days=-10, seconds=2, microseconds=400000)" % name)
+ self.assertEqual(repr(self.theclass(seconds=60)),
+ "%s(seconds=60)" % name)
+ self.assertEqual(repr(self.theclass()),
+ "%s(0)" % name)
+ self.assertEqual(repr(self.theclass(microseconds=100)),
+ "%s(microseconds=100)" % name)
+ self.assertEqual(repr(self.theclass(days=1, microseconds=100)),
+ "%s(days=1, microseconds=100)" % name)
+ self.assertEqual(repr(self.theclass(seconds=1, microseconds=100)),
+ "%s(seconds=1, microseconds=100)" % name)
def test_roundtrip(self):
for td in (timedelta(days=999999999, hours=23, minutes=59,
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index d659f36..bb22871 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -50,6 +50,8 @@
cls.tearDownClass = tearDownClass
all_test_classes.extend(test_classes)
+ all_test_classes.extend(test_classes)
+
def test_main():
run_unittest(*all_test_classes)