]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
BPO-27639: Correct return type for UserList slicing operation (#13203)
authorMichael Blahay <mblahay@users.noreply.github.com>
Sun, 26 May 2019 14:28:09 +0000 (10:28 -0400)
committerMark Shannon <mark@hotpy.org>
Sun, 26 May 2019 14:28:09 +0000 (15:28 +0100)
Added logic to __getitem__ magic method for UserList to ensure that the return
type matches that of self.

Lib/collections/__init__.py
Lib/test/test_userlist.py
Misc/NEWS.d/next/Core and Builtins/2019-05-07-15-49-17.bpo-27639.b1Ah87.rst [new file with mode: 0644]

index b4592f983921d9ff9c546bb1eee3dc994909addc..64bbee8faba8978f2fed8fdd693572b4d96885e9 100644 (file)
@@ -1091,7 +1091,11 @@ class UserList(_collections_abc.MutableSequence):
         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):
index 8de6c14e392f203f8ebf1892feb96c4c09f61db8..1ed67dac805967c4673245d29955ee394910e9c6 100644 (file)
@@ -17,6 +17,12 @@ class UserListTest(list_tests.CommonTest):
             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__)
+        self.assertEqual(u[:],u)
+
     def test_add_specials(self):
         u = UserList("spam")
         u2 = u + "eggs"
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 (file)
index 0000000..ae5b915
--- /dev/null
@@ -0,0 +1,2 @@
+Correct return type for UserList slicing operations. Patch by Michael Blahay,
+Erick Cervantes, and vaultah