Skip to content

Commit

Permalink
pythongh-109956: Also test typing.NamedTuple with copy.replace() (pyt…
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Oct 3, 2023
1 parent 8c07137 commit bb2e96f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Lib/test/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,24 @@ def __replace__(self, **changes):

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
Expand Down

0 comments on commit bb2e96f

Please sign in to comment.