This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: subclassing builtin class (str, unicode, list...) needs to override __getslice__
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, flox
Priority: normal Keywords:

Created on 2010-04-25 17:01 by flox, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg104148 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-04-25 17:01
It looks like a bug, because __getslice__ is deprecated since 2.0.

If you subclass a builtin type and override the __getitem__ method, you need to override the (deprecated) __getslice__ method too.
And if you run your program with "python -3", it 

Example script:

class Upper(unicode):

    def __getitem__(self, index):
        return unicode.__getitem__(self, index).upper()

    #def __getslice__(self, i, j):
        #return self[i:j:]

if __name__ == '__main__':
    text = Upper('Lorem ipsum')

    print text[:]
    print text[::]
msg104170 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-04-25 21:44
This is because unicode implements __getslice__.
msg104171 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-04-25 22:17
OK, but it yields Python 3 DeprecationWarning in the subclass.
And there's no workaround to get rid of the deprecation. 

If it is the correct behaviour, maybe some words could be added about subclassing builtin types:
msg104172 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-04-25 22:18
OK, I said nothing, it is already in the doc.

Date User Action Args
2022-04-11 14:57:00adminsetgithub: 52775
2010-04-25 22:18:30floxsetmessages: + msg104172
2010-04-25 22:17:28floxsetmessages: + msg104171
2010-04-25 21:44:39benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg104170

resolution: not a bug
2010-04-25 17:01:51floxcreate