[pypy-commit] pypy default: Like CPython, use type(self) to create new instances in datetime.replace().

amauryfa pypy.commits at gmail.com
Fri Jul 1 12:58:58 EDT 2016


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r85497:7a86c69fafb6
Date: 2016-07-01 18:56 +0200
http://bitbucket.org/pypy/pypy/changeset/7a86c69fafb6/

Log:	Like CPython, use type(self) to create new instances in
	datetime.replace().

diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -839,7 +839,7 @@
             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.
 
@@ -1356,7 +1356,7 @@
             microsecond = self.microsecond
         if tzinfo is True:
             tzinfo = self.tzinfo
-        return time(hour, minute, second, microsecond, tzinfo)
+        return type(self)(hour, minute, second, microsecond, tzinfo)
 
     def __nonzero__(self):
         if self.second or self.microsecond:
@@ -1566,7 +1566,7 @@
             microsecond = self.microsecond
         if tzinfo is True:
             tzinfo = self.tzinfo
-        return datetime(year, month, day, hour, minute, second, microsecond,
+        return type(self)(year, month, day, hour, minute, second, microsecond,
                         tzinfo)
 
     def astimezone(self, tz):
diff --git a/pypy/module/test_lib_pypy/test_datetime.py b/pypy/module/test_lib_pypy/test_datetime.py
--- a/pypy/module/test_lib_pypy/test_datetime.py
+++ b/pypy/module/test_lib_pypy/test_datetime.py
@@ -315,6 +315,14 @@
         class sub(datetime.timedelta): pass
         assert type(+sub()) is datetime.timedelta
 
+    def test_subclass(self):
+        class MyDate(datetime.date): pass
+        class MyTime(datetime.time): pass
+        class MyDateTime(datetime.datetime): pass
+        assert type(MyDate.today().replace(day=1)) is MyDate
+        assert type(MyTime().replace(hour=1)) is MyTime
+        assert type(MyDateTime.now().replace(day=1, hour=1)) is MyDateTime
+
 
 class TestDatetimeHost(BaseTestDatetime):
     def setup_class(cls):


More information about the pypy-commit mailing list