Skip to content

bpo-27639: Correct return type for UserList slicing operation #13169

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
May 7, 2019
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_userlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ 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__)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test that u[:] == u, thanks.

self.assertEqual(u[:],u)

def test_add_specials(self):
u = UserList("spam")
u2 = u + "eggs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Correct return type for UserList slicing operations. Patch by Michael Blahay,
Erick Cervantes, and vaultah