mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
gh-108751: Add copy.replace() function (GH-108752)
It creates a modified copy of an object by calling the object's __replace__() method. It is a generalization of dataclasses.replace(), named tuple's _replace() method and replace() methods in various classes, and supports all these stdlib classes.
This commit is contained in:
parent
9f0c0a46f0
commit
6f3c138dfa
19 changed files with 311 additions and 68 deletions
|
@ -1699,22 +1699,23 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
|
|||
cls = self.theclass
|
||||
args = [1, 2, 3]
|
||||
base = cls(*args)
|
||||
self.assertEqual(base, base.replace())
|
||||
self.assertEqual(base.replace(), base)
|
||||
self.assertEqual(copy.replace(base), base)
|
||||
|
||||
i = 0
|
||||
for name, newval in (("year", 2),
|
||||
("month", 3),
|
||||
("day", 4)):
|
||||
changes = (("year", 2),
|
||||
("month", 3),
|
||||
("day", 4))
|
||||
for i, (name, newval) in enumerate(changes):
|
||||
newargs = args[:]
|
||||
newargs[i] = newval
|
||||
expected = cls(*newargs)
|
||||
got = base.replace(**{name: newval})
|
||||
self.assertEqual(expected, got)
|
||||
i += 1
|
||||
self.assertEqual(base.replace(**{name: newval}), expected)
|
||||
self.assertEqual(copy.replace(base, **{name: newval}), expected)
|
||||
|
||||
# Out of bounds.
|
||||
base = cls(2000, 2, 29)
|
||||
self.assertRaises(ValueError, base.replace, year=2001)
|
||||
self.assertRaises(ValueError, copy.replace, base, year=2001)
|
||||
|
||||
def test_subclass_replace(self):
|
||||
class DateSubclass(self.theclass):
|
||||
|
@ -1722,6 +1723,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
|
|||
|
||||
dt = DateSubclass(2012, 1, 1)
|
||||
self.assertIs(type(dt.replace(year=2013)), DateSubclass)
|
||||
self.assertIs(type(copy.replace(dt, year=2013)), DateSubclass)
|
||||
|
||||
def test_subclass_date(self):
|
||||
|
||||
|
@ -2856,26 +2858,27 @@ class TestDateTime(TestDate):
|
|||
cls = self.theclass
|
||||
args = [1, 2, 3, 4, 5, 6, 7]
|
||||
base = cls(*args)
|
||||
self.assertEqual(base, base.replace())
|
||||
self.assertEqual(base.replace(), base)
|
||||
self.assertEqual(copy.replace(base), base)
|
||||
|
||||
i = 0
|
||||
for name, newval in (("year", 2),
|
||||
("month", 3),
|
||||
("day", 4),
|
||||
("hour", 5),
|
||||
("minute", 6),
|
||||
("second", 7),
|
||||
("microsecond", 8)):
|
||||
changes = (("year", 2),
|
||||
("month", 3),
|
||||
("day", 4),
|
||||
("hour", 5),
|
||||
("minute", 6),
|
||||
("second", 7),
|
||||
("microsecond", 8))
|
||||
for i, (name, newval) in enumerate(changes):
|
||||
newargs = args[:]
|
||||
newargs[i] = newval
|
||||
expected = cls(*newargs)
|
||||
got = base.replace(**{name: newval})
|
||||
self.assertEqual(expected, got)
|
||||
i += 1
|
||||
self.assertEqual(base.replace(**{name: newval}), expected)
|
||||
self.assertEqual(copy.replace(base, **{name: newval}), expected)
|
||||
|
||||
# Out of bounds.
|
||||
base = cls(2000, 2, 29)
|
||||
self.assertRaises(ValueError, base.replace, year=2001)
|
||||
self.assertRaises(ValueError, copy.replace, base, year=2001)
|
||||
|
||||
@support.run_with_tz('EDT4')
|
||||
def test_astimezone(self):
|
||||
|
@ -3671,19 +3674,19 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
|
|||
cls = self.theclass
|
||||
args = [1, 2, 3, 4]
|
||||
base = cls(*args)
|
||||
self.assertEqual(base, base.replace())
|
||||
self.assertEqual(base.replace(), base)
|
||||
self.assertEqual(copy.replace(base), base)
|
||||
|
||||
i = 0
|
||||
for name, newval in (("hour", 5),
|
||||
("minute", 6),
|
||||
("second", 7),
|
||||
("microsecond", 8)):
|
||||
changes = (("hour", 5),
|
||||
("minute", 6),
|
||||
("second", 7),
|
||||
("microsecond", 8))
|
||||
for i, (name, newval) in enumerate(changes):
|
||||
newargs = args[:]
|
||||
newargs[i] = newval
|
||||
expected = cls(*newargs)
|
||||
got = base.replace(**{name: newval})
|
||||
self.assertEqual(expected, got)
|
||||
i += 1
|
||||
self.assertEqual(base.replace(**{name: newval}), expected)
|
||||
self.assertEqual(copy.replace(base, **{name: newval}), expected)
|
||||
|
||||
# Out of bounds.
|
||||
base = cls(1)
|
||||
|
@ -3691,6 +3694,10 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
|
|||
self.assertRaises(ValueError, base.replace, minute=-1)
|
||||
self.assertRaises(ValueError, base.replace, second=100)
|
||||
self.assertRaises(ValueError, base.replace, microsecond=1000000)
|
||||
self.assertRaises(ValueError, copy.replace, base, hour=24)
|
||||
self.assertRaises(ValueError, copy.replace, base, minute=-1)
|
||||
self.assertRaises(ValueError, copy.replace, base, second=100)
|
||||
self.assertRaises(ValueError, copy.replace, base, microsecond=1000000)
|
||||
|
||||
def test_subclass_replace(self):
|
||||
class TimeSubclass(self.theclass):
|
||||
|
@ -3698,6 +3705,7 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
|
|||
|
||||
ctime = TimeSubclass(12, 30)
|
||||
self.assertIs(type(ctime.replace(hour=10)), TimeSubclass)
|
||||
self.assertIs(type(copy.replace(ctime, hour=10)), TimeSubclass)
|
||||
|
||||
def test_subclass_time(self):
|
||||
|
||||
|
@ -4085,31 +4093,37 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
|
|||
zm200 = FixedOffset(timedelta(minutes=-200), "-200")
|
||||
args = [1, 2, 3, 4, z100]
|
||||
base = cls(*args)
|
||||
self.assertEqual(base, base.replace())
|
||||
self.assertEqual(base.replace(), base)
|
||||
self.assertEqual(copy.replace(base), base)
|
||||
|
||||
i = 0
|
||||
for name, newval in (("hour", 5),
|
||||
("minute", 6),
|
||||
("second", 7),
|
||||
("microsecond", 8),
|
||||
("tzinfo", zm200)):
|
||||
changes = (("hour", 5),
|
||||
("minute", 6),
|
||||
("second", 7),
|
||||
("microsecond", 8),
|
||||
("tzinfo", zm200))
|
||||
for i, (name, newval) in enumerate(changes):
|
||||
newargs = args[:]
|
||||
newargs[i] = newval
|
||||
expected = cls(*newargs)
|
||||
got = base.replace(**{name: newval})
|
||||
self.assertEqual(expected, got)
|
||||
i += 1
|
||||
self.assertEqual(base.replace(**{name: newval}), expected)
|
||||
self.assertEqual(copy.replace(base, **{name: newval}), expected)
|
||||
|
||||
# Ensure we can get rid of a tzinfo.
|
||||
self.assertEqual(base.tzname(), "+100")
|
||||
base2 = base.replace(tzinfo=None)
|
||||
self.assertIsNone(base2.tzinfo)
|
||||
self.assertIsNone(base2.tzname())
|
||||
base22 = copy.replace(base, tzinfo=None)
|
||||
self.assertIsNone(base22.tzinfo)
|
||||
self.assertIsNone(base22.tzname())
|
||||
|
||||
# Ensure we can add one.
|
||||
base3 = base2.replace(tzinfo=z100)
|
||||
self.assertEqual(base, base3)
|
||||
self.assertIs(base.tzinfo, base3.tzinfo)
|
||||
base32 = copy.replace(base22, tzinfo=z100)
|
||||
self.assertEqual(base, base32)
|
||||
self.assertIs(base.tzinfo, base32.tzinfo)
|
||||
|
||||
# Out of bounds.
|
||||
base = cls(1)
|
||||
|
@ -4117,6 +4131,10 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
|
|||
self.assertRaises(ValueError, base.replace, minute=-1)
|
||||
self.assertRaises(ValueError, base.replace, second=100)
|
||||
self.assertRaises(ValueError, base.replace, microsecond=1000000)
|
||||
self.assertRaises(ValueError, copy.replace, base, hour=24)
|
||||
self.assertRaises(ValueError, copy.replace, base, minute=-1)
|
||||
self.assertRaises(ValueError, copy.replace, base, second=100)
|
||||
self.assertRaises(ValueError, copy.replace, base, microsecond=1000000)
|
||||
|
||||
def test_mixed_compare(self):
|
||||
t1 = self.theclass(1, 2, 3)
|
||||
|
@ -4885,38 +4903,45 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
|
|||
zm200 = FixedOffset(timedelta(minutes=-200), "-200")
|
||||
args = [1, 2, 3, 4, 5, 6, 7, z100]
|
||||
base = cls(*args)
|
||||
self.assertEqual(base, base.replace())
|
||||
self.assertEqual(base.replace(), base)
|
||||
self.assertEqual(copy.replace(base), base)
|
||||
|
||||
i = 0
|
||||
for name, newval in (("year", 2),
|
||||
("month", 3),
|
||||
("day", 4),
|
||||
("hour", 5),
|
||||
("minute", 6),
|
||||
("second", 7),
|
||||
("microsecond", 8),
|
||||
("tzinfo", zm200)):
|
||||
changes = (("year", 2),
|
||||
("month", 3),
|
||||
("day", 4),
|
||||
("hour", 5),
|
||||
("minute", 6),
|
||||
("second", 7),
|
||||
("microsecond", 8),
|
||||
("tzinfo", zm200))
|
||||
for i, (name, newval) in enumerate(changes):
|
||||
newargs = args[:]
|
||||
newargs[i] = newval
|
||||
expected = cls(*newargs)
|
||||
got = base.replace(**{name: newval})
|
||||
self.assertEqual(expected, got)
|
||||
i += 1
|
||||
self.assertEqual(base.replace(**{name: newval}), expected)
|
||||
self.assertEqual(copy.replace(base, **{name: newval}), expected)
|
||||
|
||||
# Ensure we can get rid of a tzinfo.
|
||||
self.assertEqual(base.tzname(), "+100")
|
||||
base2 = base.replace(tzinfo=None)
|
||||
self.assertIsNone(base2.tzinfo)
|
||||
self.assertIsNone(base2.tzname())
|
||||
base22 = copy.replace(base, tzinfo=None)
|
||||
self.assertIsNone(base22.tzinfo)
|
||||
self.assertIsNone(base22.tzname())
|
||||
|
||||
# Ensure we can add one.
|
||||
base3 = base2.replace(tzinfo=z100)
|
||||
self.assertEqual(base, base3)
|
||||
self.assertIs(base.tzinfo, base3.tzinfo)
|
||||
base32 = copy.replace(base22, tzinfo=z100)
|
||||
self.assertEqual(base, base32)
|
||||
self.assertIs(base.tzinfo, base32.tzinfo)
|
||||
|
||||
# Out of bounds.
|
||||
base = cls(2000, 2, 29)
|
||||
self.assertRaises(ValueError, base.replace, year=2001)
|
||||
self.assertRaises(ValueError, copy.replace, base, year=2001)
|
||||
|
||||
def test_more_astimezone(self):
|
||||
# The inherited test_astimezone covered some trivial and error cases.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue