[Python-checkins] bpo-31222: Make (datetime|date|time).replace return subclass type in Pure Python (GH-4176) (#4356)

Victor Stinner webhook-mailer at python.org
Thu Nov 9 19:52:08 EST 2017


https://github.com/python/cpython/commit/b9a40aca2935d2569191844c88f8b61269e383cb
commit: b9a40aca2935d2569191844c88f8b61269e383cb
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Victor Stinner <victor.stinner at gmail.com>
date: 2017-11-09T16:52:05-08:00
summary:

bpo-31222: Make (datetime|date|time).replace return subclass type in Pure Python (GH-4176) (#4356)

(cherry picked from commit 191e993365ac3206f46132dcf46236471ec54bfa)

files:
M Lib/datetime.py
M Lib/test/datetimetester.py

diff --git a/Lib/datetime.py b/Lib/datetime.py
index b95536fb7af..150664ea3b6 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -827,7 +827,7 @@ def replace(self, year=None, month=None, day=None):
             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.
 
@@ -1315,7 +1315,7 @@ def replace(self, hour=None, minute=None, second=None, microsecond=None,
             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.
 
@@ -1596,7 +1596,7 @@ def replace(self, year=None, month=None, day=None, hour=None,
             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):
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index c00e38cb0c0..f23a5305e45 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -1500,6 +1500,13 @@ def test_replace(self):
         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):
@@ -2599,6 +2606,13 @@ def test_replace(self):
         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):



More information about the Python-checkins mailing list