summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-09-10 05:09:25 (GMT)
committerGitHub <noreply@github.com>2023-09-10 05:09:25 (GMT)
commit92578919a60ebe2b8d6d42377f1e27479c156d65 (patch)
tree39e90cc7a5130e034130e23ff01b34a855626411 /Lib/test
parent0eab2427b149cd46e0dee3efbb6b2cfca2a4f723 (diff)
downloadcpython-92578919a60ebe2b8d6d42377f1e27479c156d65.zip
cpython-92578919a60ebe2b8d6d42377f1e27479c156d65.tar.gz
cpython-92578919a60ebe2b8d6d42377f1e27479c156d65.tar.bz2
gh-109174: Add support of SimpleNamespace in copy.replace() (GH-109175)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_types.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index f2efee9..c6bff79 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -1900,6 +1900,33 @@ class SimpleNamespaceTests(unittest.TestCase):
self.assertEqual(ns, ns_roundtrip, pname)
+ def test_replace(self):
+ ns = types.SimpleNamespace(x=11, y=22)
+
+ ns2 = copy.replace(ns)
+ self.assertEqual(ns2, ns)
+ self.assertIsNot(ns2, ns)
+ self.assertIs(type(ns2), types.SimpleNamespace)
+ self.assertEqual(vars(ns2), {'x': 11, 'y': 22})
+ ns2.x = 3
+ self.assertEqual(ns.x, 11)
+ ns.x = 4
+ self.assertEqual(ns2.x, 3)
+
+ self.assertEqual(vars(copy.replace(ns, x=1)), {'x': 1, 'y': 22})
+ self.assertEqual(vars(copy.replace(ns, y=2)), {'x': 4, 'y': 2})
+ self.assertEqual(vars(copy.replace(ns, x=1, y=2)), {'x': 1, 'y': 2})
+
+ def test_replace_subclass(self):
+ class Spam(types.SimpleNamespace):
+ pass
+
+ spam = Spam(ham=8, eggs=9)
+ spam2 = copy.replace(spam, ham=5)
+
+ self.assertIs(type(spam2), Spam)
+ self.assertEqual(vars(spam2), {'ham': 5, 'eggs': 9})
+
def test_fake_namespace_compare(self):
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
# SystemError.