-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
3556649
359b007
6cee271
5893ba7
3f46389
db068d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
self.assertEqual(res, [1, 2, 'a', 'b']) | ||
self.assertIsInstance(data1, ListSubclass) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -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: | ||
|
There was a problem hiding this comment.
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.