-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
UserList.__getitem__ doesn't account for slices #71826
Comments
The docs for UserList say:
However, UserList([])[:] returns a list, not a UserList. |
LGTM, but this is change we should probably only make in a feature release, with a note in the What's New porting section. |
Take a look at the following code from UserList.py in Python2.7 to get an idea of how this was implemented previously: def __getslice__(self, i, j):
i = max(i, 0); j = max(j, 0)
return self.__class__(self.data[i:j])
def __setslice__(self, i, j, other):
i = max(i, 0); j = max(j, 0)
if isinstance(other, UserList):
self.data[i:j] = other.data
elif isinstance(other, type(self.data)):
self.data[i:j] = other
else:
self.data[i:j] = list(other)
def __delslice__(self, i, j):
i = max(i, 0); j = max(j, 0)
del self.data[i:j] |
Here is a test that more explicitly shows the problem. >>> from collections import UserList
>>> UserList([0,1,2,3,4,5])[0:2].__class__
<class 'list'>
>>> It can clearly be seen here that the return type of the slicing operator is list when it should, in this case, be UserList (or whatever class one is using that is derived from UserList). |
The root cause of this issue seems to be the failure to implement type usage in __getitem__ when the deprecated __getslice__ was removed. This is why slicing worked correctly in 2.7, but not the 3.x versions. In 3.8, the __getitem__ method is used to create the slice, but here we can see that all it does is pass the task to data, which is of type list and then fails to convert the result to the correct type. def __getitem__(self, i): return self.data[i] Using other methods as examples, the fix should look like this: def __getitem__(self, i): return self.__class__(self.data[i]) |
It is also worth noting that the definition of UserList moved from Lib/UserList.py in 2.7 to Lib/collections/init.py in 3.x |
Results from a quick unit test on the proposed changes were positive: >>> from collections import UserList
>>> UserList([0,1,2,3,4,5])[0:2].__class__
<class 'collections.UserList'> If you compare this result with the one a couple comments above, you can see that the result is no longer a list, but rather of type UserList. |
PR 4981 LGTM (except that it would be worth to add the author's name in the NEWS entry). It is sad that it was not reviewed for 2 years and is closed now. |
Thank you for bringing up that PR. My team will review and try to find out why it was closed without a merge. |
Please note that I am working with Erick Cervantes at PyCon2019 on this issue. |
Let it be known that the author of PR 4981 is known as vaultah. He/she personally closed the pull request this morning stating a lack of need to be recognized for the work. Per his/her instructions I am reviewing the changes and incorporating in our solution as needed. Thank you vaultah! |
The real name of vaultah is Dmitry Kazakov. His PR LGTM. Did you notice that I recreated the original PR? |
PR 13150, the recreation of PR 4981, was noticed just after I created PR 13169. It was decided to continue forward with PR 13169 since PR 13150 contained changes that were out of scope for BPO-27639 for which it was suspected might have been the cause of the non-merging of PR 4981. Thank for identifying Dmitry; I did my best to give credit where credit was due. |
For those that are wondering what is going on with PR 13181 and PR 13203, those are both for back porting the change to 3.7. The auto generated PR 13181 failed for an unknown reason and then the bot deleted the branch so the PR could not be taken any further. PR 13203 is a manual attempt at the backport which is moving along much for successfully. |
PR 13203 has finally made it through all checks successfully and is now awaiting merging. Please someone pick it up and merge it. Thank you |
PR 13203 is still waiting for merge |
1 similar comment
PR 13203 is still waiting for merge |
Thank you Mark. Everything for this one is complete. The issue may be closed. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: