]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-31222: Make (datetime|date|time).replace return subclass type in Pure Python...
authorPaul Ganssle <pganssle@users.noreply.github.com>
Thu, 9 Nov 2017 21:34:29 +0000 (16:34 -0500)
committerVictor Stinner <victor.stinner@gmail.com>
Thu, 9 Nov 2017 21:34:29 +0000 (13:34 -0800)
Lib/datetime.py
Lib/test/datetimetester.py

index 2f03847be0a0794c14aa1a67d150ad4ad381faf3..67d8600921c382b4b280a7afced2f5e480069e5f 100644 (file)
@@ -828,7 +828,7 @@ class date:
             month = self._month
         if day is None:
             day = self._day
-        return date(year, month, day)
+        return type(self)(year, month, day)
 
     # Comparisons of date objects with other.
 
@@ -1316,7 +1316,7 @@ class time:
             tzinfo = self.tzinfo
         if fold is None:
             fold = self._fold
-        return time(hour, minute, second, microsecond, tzinfo, fold=fold)
+        return type(self)(hour, minute, second, microsecond, tzinfo, fold=fold)
 
     # Pickle support.
 
@@ -1597,7 +1597,7 @@ class datetime(date):
             tzinfo = self.tzinfo
         if fold is None:
             fold = self.fold
-        return datetime(year, month, day, hour, minute, second,
+        return type(self)(year, month, day, hour, minute, second,
                           microsecond, tzinfo, fold=fold)
 
     def _local_timezone(self):
index c5f91fbe1840baef1c52b70c7e53a715ebcd5588..5b58e99fbcd3573c17a0d6887c3bc82cc13388fc 100644 (file)
@@ -1520,6 +1520,13 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
         base = cls(2000, 2, 29)
         self.assertRaises(ValueError, base.replace, year=2001)
 
+    def test_subclass_replace(self):
+        class DateSubclass(self.theclass):
+            pass
+
+        dt = DateSubclass(2012, 1, 1)
+        self.assertIs(type(dt.replace(year=2013)), DateSubclass)
+
     def test_subclass_date(self):
 
         class C(self.theclass):
@@ -2626,6 +2633,13 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
         self.assertRaises(ValueError, base.replace, second=100)
         self.assertRaises(ValueError, base.replace, microsecond=1000000)
 
+    def test_subclass_replace(self):
+        class TimeSubclass(self.theclass):
+            pass
+
+        ctime = TimeSubclass(12, 30)
+        self.assertIs(type(ctime.replace(hour=10)), TimeSubclass)
+
     def test_subclass_time(self):
 
         class C(self.theclass):