Kaydet (Commit) b9a40aca authored tarafından Miss Islington (bot)'s avatar Miss Islington (bot) Kaydeden (comit) Victor Stinner

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

(cherry picked from commit 191e9933)
üst 596286f8
...@@ -827,7 +827,7 @@ class date: ...@@ -827,7 +827,7 @@ class date:
month = self._month month = self._month
if day is None: if day is None:
day = self._day day = self._day
return date(year, month, day) return type(self)(year, month, day)
# Comparisons of date objects with other. # Comparisons of date objects with other.
...@@ -1315,7 +1315,7 @@ class time: ...@@ -1315,7 +1315,7 @@ class time:
tzinfo = self.tzinfo tzinfo = self.tzinfo
if fold is None: if fold is None:
fold = self._fold fold = self._fold
return time(hour, minute, second, microsecond, tzinfo, fold=fold) return type(self)(hour, minute, second, microsecond, tzinfo, fold=fold)
# Pickle support. # Pickle support.
...@@ -1596,7 +1596,7 @@ class datetime(date): ...@@ -1596,7 +1596,7 @@ class datetime(date):
tzinfo = self.tzinfo tzinfo = self.tzinfo
if fold is None: if fold is None:
fold = self.fold fold = self.fold
return datetime(year, month, day, hour, minute, second, return type(self)(year, month, day, hour, minute, second,
microsecond, tzinfo, fold=fold) microsecond, tzinfo, fold=fold)
def _local_timezone(self): def _local_timezone(self):
......
...@@ -1500,6 +1500,13 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase): ...@@ -1500,6 +1500,13 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
base = cls(2000, 2, 29) base = cls(2000, 2, 29)
self.assertRaises(ValueError, base.replace, year=2001) 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): def test_subclass_date(self):
class C(self.theclass): class C(self.theclass):
...@@ -2599,6 +2606,13 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase): ...@@ -2599,6 +2606,13 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
self.assertRaises(ValueError, base.replace, second=100) self.assertRaises(ValueError, base.replace, second=100)
self.assertRaises(ValueError, base.replace, microsecond=1000000) 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): def test_subclass_time(self):
class C(self.theclass): class C(self.theclass):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment