Skip to content

gh-94808: add tests covering PySequence_[InPlace]Concat #99319

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
40 changes: 26 additions & 14 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,27 +531,33 @@ def test_concat(self):
self.assertEqual(data2, 'ab') # must not change

# Subclasses:
class ListSubclass(list): ...
class ListSubclass(list):
pass

data1 = ListSubclass([1, 2])
data2 = ListSubclass(['a', 'b'])
self.assertEqual(operator.concat(data1, data2),
ListSubclass([1, 2, 'a', 'b']))
self.assertEqual(operator.concat(data1, data2), data1 + data2)

res = operator.concat(data1, data2)
self.assertIsInstance(res, list)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't prove anything, does it? Whether the result is a ListSubclass or a plain list, this will always be true. If you want to say something interesting here I'd assert that it isn't a ListSubclass instance.

self.assertEqual(res, [1, 2, 'a', 'b'])
self.assertIsInstance(data1, ListSubclass)
Copy link
Member

Choose a reason for hiding this comment

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

I don't see the point of this -- of course data1 and data2 are instances of ListSubclass, that's how they were created.

self.assertEqual(data1, ListSubclass([1, 2])) # must not change
self.assertIsInstance(data2, ListSubclass)
self.assertEqual(data2, ListSubclass(['a', 'b'])) # must not change

# Custom type with `__add__`:
class TupleSubclass(tuple):
def __add__(self, other):
return other + self
return TupleSubclass(other + self)

data1 = TupleSubclass([1, 2])
data2 = ('a', 'b')
self.assertEqual(operator.concat(data1, data2),
TupleSubclass(['a', 'b', 1, 2]))
self.assertEqual(operator.concat(data1, data2), data1 + data2)
res = operator.concat(data1, data2)
self.assertIsInstance(res, TupleSubclass)
Copy link
Member

Choose a reason for hiding this comment

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

This one is good.

self.assertEqual(res, TupleSubclass(['a', 'b', 1, 2]))
self.assertIsInstance(data1, TupleSubclass)
Copy link
Member

Choose a reason for hiding this comment

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

But this is still questionable. I can't think of a scenario where this would fail.

Or do you have coverage results showing this is needed? (Where?)

Ditto for data2 and again in the following block of tests.

self.assertEqual(data1, TupleSubclass([1, 2])) # must not change
self.assertIsInstance(data2, tuple)
self.assertEqual(data2, ('a', 'b')) # must not change

# Corner cases:
Expand Down Expand Up @@ -599,26 +605,32 @@ def test_iconcat(self):
self.assertEqual(data2, 'ab') # must not change

# Subclasses:
class ListSubclass(list): ...
class ListSubclass(list):
pass

data1 = ListSubclass([1, 2])
data2 = ListSubclass(['a', 'b'])
res = operator.iconcat(data1, data2)
self.assertEqual(res, ListSubclass([1, 2, 'a', 'b']))
self.assertIsInstance(res, ListSubclass)
self.assertEqual(res, [1, 2, 'a', 'b'])
self.assertIsInstance(data1, ListSubclass)
self.assertEqual(data1, ListSubclass([1, 2, 'a', 'b'])) # must change
self.assertIsInstance(data2, ListSubclass)
self.assertEqual(data2, ListSubclass(['a', 'b'])) # must not change

# Custom type with `__add__`:
class TupleSubclass(tuple):
def __add__(self, other):
return other + self
return TupleSubclass(other + self)

data1 = TupleSubclass([1, 2])
data2 = ('a', 'b')
self.assertEqual(operator.iconcat(data1, data2),
TupleSubclass(['a', 'b', 1, 2]))
self.assertEqual(operator.iconcat(data1, data2), data1 + data2)
res = operator.iconcat(data1, data2)
self.assertIsInstance(res, TupleSubclass)
self.assertEqual(res, TupleSubclass(['a', 'b', 1, 2]))
self.assertIsInstance(data1, TupleSubclass)
self.assertEqual(data1, TupleSubclass([1, 2])) # must not change
self.assertIsInstance(data2, tuple)
self.assertEqual(data2, ('a', 'b')) # must not change

# Corner cases:
Expand Down