Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-109956: Also test typing.NamedTuple with copy.replace #109957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test the class?

Suggested change
p = Point(11, 22)
p = Point(11, 22)
assert isinstance(copy.replace(p, x=12), Point)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? :)
Done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I meant to test the copy, not the original p!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not notice this conversation (I can't see the text few lines above the line I'm looking at), but I added a test for a copy just before merging. I was puzzled as to why the test for the original was added, but decided not to drag the review out.

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