From 73eb4129ed2d76fbda6ec1c09a531826929e66a6 Mon Sep 17 00:00:00 2001 From: Michael Blahay Date: Tue, 7 May 2019 15:05:42 -0400 Subject: [PATCH 1/3] BPO-27639: Correct return type for UserList slicing operation Added logic to __getitem__ magic method for UserList to ensure that the return type matches that of self. --- Lib/collections/__init__.py | 6 +++++- Lib/test/test_userlist.py | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index cff75a48d62780..5c043acdc98843 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1083,7 +1083,11 @@ def __cast(self, other): return other.data if isinstance(other, UserList) else other def __contains__(self, item): return item in self.data def __len__(self): return len(self.data) - def __getitem__(self, i): return self.data[i] + def __getitem__(self, i): + if isinstance(i, slice): + return self.__class__(self.data[i]) + else: + return self.data[i] def __setitem__(self, i, item): self.data[i] = item def __delitem__(self, i): del self.data[i] def __add__(self, other): diff --git a/Lib/test/test_userlist.py b/Lib/test/test_userlist.py index 8de6c14e392f20..5adc126f369e38 100644 --- a/Lib/test/test_userlist.py +++ b/Lib/test/test_userlist.py @@ -17,6 +17,11 @@ def test_getslice(self): for j in range(-3, 6): self.assertEqual(u[i:j], l[i:j]) + def test_slice_type(self): + l = [0, 1, 2, 3, 4] + u = UserList(l) + self.assertIsInstance(u[:], u.__class__) + def test_add_specials(self): u = UserList("spam") u2 = u + "eggs" From 5d4de39872dc3fc0c4851bb4d98dbabec1576680 Mon Sep 17 00:00:00 2001 From: Michael Blahay Date: Tue, 7 May 2019 15:56:09 -0400 Subject: [PATCH 2/3] BPO-27639: Adding news entry --- .../Core and Builtins/2019-05-07-15-49-17.bpo-27639.b1Ah87.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-05-07-15-49-17.bpo-27639.b1Ah87.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-05-07-15-49-17.bpo-27639.b1Ah87.rst b/Misc/NEWS.d/next/Core and Builtins/2019-05-07-15-49-17.bpo-27639.b1Ah87.rst new file mode 100644 index 00000000000000..ae5b915969d3b5 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-05-07-15-49-17.bpo-27639.b1Ah87.rst @@ -0,0 +1,2 @@ +Correct return type for UserList slicing operations. Patch by Michael Blahay, +Erick Cervantes, and vaultah From 24a57461ad3728db97b5fba76dda94d1958dad2a Mon Sep 17 00:00:00 2001 From: Michael Blahay Date: Tue, 7 May 2019 16:13:22 -0400 Subject: [PATCH 3/3] BPO-27639: Adding test suggested by reviewer --- Lib/test/test_userlist.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_userlist.py b/Lib/test/test_userlist.py index 5adc126f369e38..1ed67dac805967 100644 --- a/Lib/test/test_userlist.py +++ b/Lib/test/test_userlist.py @@ -21,6 +21,7 @@ def test_slice_type(self): l = [0, 1, 2, 3, 4] u = UserList(l) self.assertIsInstance(u[:], u.__class__) + self.assertEqual(u[:],u) def test_add_specials(self): u = UserList("spam")