MCPcopy Index your code
hub / github.com/python/cpython / test_namedtuple

Method test_namedtuple

Lib/test/test_copy.py:973–992  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

971 self.assertEqual(attrs(copy.replace(a, x=1, y=2)), (1, 2, 3))
972
973 def test_namedtuple(self):
974 from collections import namedtuple
975 from typing import NamedTuple
976 PointFromCall = namedtuple('Point', 'x y', defaults=(0,))
977 class PointFromInheritance(PointFromCall):
978 pass
979 class PointFromClass(NamedTuple):
980 x: int
981 y: int = 0
982 for Point in (PointFromCall, PointFromInheritance, PointFromClass):
983 with self.subTest(Point=Point):
984 p = Point(11, 22)
985 self.assertIsInstance(p, Point)
986 self.assertEqual(copy.replace(p), (11, 22))
987 self.assertIsInstance(copy.replace(p), Point)
988 self.assertEqual(copy.replace(p, x=1), (1, 22))
989 self.assertEqual(copy.replace(p, y=2), (11, 2))
990 self.assertEqual(copy.replace(p, x=1, y=2), (1, 2))
991 with self.assertRaisesRegex(TypeError, 'unexpected field name'):
992 copy.replace(p, x=1, error=2)
993
994 def test_dataclass(self):
995 from dataclasses import dataclass

Callers

nothing calls this directly

Calls 7

namedtupleFunction · 0.90
assertIsInstanceMethod · 0.80
assertRaisesRegexMethod · 0.80
PointClass · 0.70
subTestMethod · 0.45
assertEqualMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected