diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2023-10-03 11:13:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-03 11:13:13 (GMT) |
commit | bb2e96f6f4d6c397c4eb5775a09262a207675577 (patch) | |
tree | 65f75ee0764ada7ee2ab7ca5af59df1b96fbc9b5 | |
parent | 8c071373f12f325c54591fe990ec026184e48f8f (diff) | |
download | cpython-bb2e96f6f4d6c397c4eb5775a09262a207675577.zip cpython-bb2e96f6f4d6c397c4eb5775a09262a207675577.tar.gz cpython-bb2e96f6f4d6c397c4eb5775a09262a207675577.tar.bz2 |
gh-109956: Also test typing.NamedTuple with copy.replace() (GH-109957)
-rw-r--r-- | Lib/test/test_copy.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index c66c6ee..60735ba 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -936,14 +936,24 @@ class TestReplace(unittest.TestCase): def test_namedtuple(self): from collections import namedtuple - Point = namedtuple('Point', 'x y', defaults=(0,)) - p = Point(11, 22) - self.assertEqual(copy.replace(p), (11, 22)) - self.assertEqual(copy.replace(p, x=1), (1, 22)) - self.assertEqual(copy.replace(p, y=2), (11, 2)) - self.assertEqual(copy.replace(p, x=1, y=2), (1, 2)) - with self.assertRaisesRegex(ValueError, 'unexpected field name'): - copy.replace(p, x=1, error=2) + from typing import NamedTuple + PointFromCall = namedtuple('Point', 'x y', defaults=(0,)) + class PointFromInheritance(PointFromCall): + pass + class PointFromClass(NamedTuple): + x: int + y: int = 0 + for Point in (PointFromCall, PointFromInheritance, PointFromClass): + with self.subTest(Point=Point): + p = Point(11, 22) + self.assertIsInstance(p, Point) + self.assertEqual(copy.replace(p), (11, 22)) + self.assertIsInstance(copy.replace(p), Point) + self.assertEqual(copy.replace(p, x=1), (1, 22)) + self.assertEqual(copy.replace(p, y=2), (11, 2)) + self.assertEqual(copy.replace(p, x=1, y=2), (1, 2)) + with self.assertRaisesRegex(ValueError, 'unexpected field name'): + copy.replace(p, x=1, error=2) def test_dataclass(self): from dataclasses import dataclass |