|
30 | 30 | ###############################################################################
|
31 | 31 |
|
32 | 32 | try:
|
33 |
| - from collections.abc import Sequence, MutableSequence |
| 33 | + from collections.abc import Sequence |
34 | 34 | except ImportError:
|
35 |
| - from collections import Sequence, MutableSequence |
| 35 | + from collections import Sequence |
36 | 36 |
|
37 | 37 | from functools import wraps
|
38 | 38 | from sys import hexversion
|
@@ -82,7 +82,7 @@ def wrapper(self):
|
82 | 82 | ###############################################################################
|
83 | 83 |
|
84 | 84 |
|
85 |
| -class SortedList(MutableSequence): |
| 85 | +class SortedList(Sequence): |
86 | 86 | """Sorted list is a sorted mutable sequence.
|
87 | 87 |
|
88 | 88 | Sorted list values are maintained in sorted order.
|
@@ -134,8 +134,14 @@ class SortedList(MutableSequence):
|
134 | 134 | Sorted lists use lexicographical ordering semantics when compared to other
|
135 | 135 | sequences.
|
136 | 136 |
|
137 |
| - Some methods of mutable sequences are not supported and will raise |
138 |
| - not-implemented error. |
| 137 | + .. versionchanged:: 3.0 |
| 138 | +
|
| 139 | + SortedLists are mutable sequences but they do not implement the |
| 140 | + :class:`collections.abc.MutableSequence` interface. In version 3.0, the |
| 141 | + base class was switched to :class:`collections.abc.Sequence` and the |
| 142 | + ``append``, ``extend``, ``reverse`` and ``insert`` methods, which |
| 143 | + previously raised :py:exc:`NotImplementedError` when called, were |
| 144 | + removed. |
139 | 145 |
|
140 | 146 | """
|
141 | 147 | DEFAULT_LOAD_FACTOR = 1000
|
@@ -932,24 +938,6 @@ def __reversed__(self):
|
932 | 938 | return chain.from_iterable(map(reversed, reversed(self._lists)))
|
933 | 939 |
|
934 | 940 |
|
935 |
| - def reverse(self): |
936 |
| - """Raise not-implemented error. |
937 |
| -
|
938 |
| - Sorted list maintains values in ascending sort order. Values may not be |
939 |
| - reversed in-place. |
940 |
| -
|
941 |
| - Use ``reversed(sl)`` for an iterator over values in descending sort |
942 |
| - order. |
943 |
| -
|
944 |
| - Implemented to override `MutableSequence.reverse` which provides an |
945 |
| - erroneous default implementation. |
946 |
| -
|
947 |
| - :raises NotImplementedError: use ``reversed(sl)`` instead |
948 |
| -
|
949 |
| - """ |
950 |
| - raise NotImplementedError('use ``reversed(sl)`` instead') |
951 |
| - |
952 |
| - |
953 | 941 | def islice(self, start=None, stop=None, reverse=False):
|
954 | 942 | """Return an iterator that slices sorted list from `start` to `stop`.
|
955 | 943 |
|
@@ -1273,38 +1261,20 @@ def copy(self):
|
1273 | 1261 |
|
1274 | 1262 | __copy__ = copy
|
1275 | 1263 |
|
| 1264 | + def __getattr__(self, key): |
| 1265 | + if key == 'append': |
| 1266 | + msg = 'use ``sl.add(value)`` instead' |
| 1267 | + elif key == 'extend': |
| 1268 | + msg = 'use ``sl.update(values)`` instead' |
| 1269 | + elif key == 'reverse': |
| 1270 | + msg = 'use ``reversed(sl)`` instead' |
| 1271 | + elif key == 'insert': |
| 1272 | + msg = 'use ``sl.add(value)`` instead' |
| 1273 | + else: |
| 1274 | + msg = "'%s' object has no attribute '%s'" % (type(self).__name__, |
| 1275 | + key) |
1276 | 1276 |
|
1277 |
| - def append(self, value): |
1278 |
| - """Raise not-implemented error. |
1279 |
| -
|
1280 |
| - Implemented to override `MutableSequence.append` which provides an |
1281 |
| - erroneous default implementation. |
1282 |
| -
|
1283 |
| - :raises NotImplementedError: use ``sl.add(value)`` instead |
1284 |
| -
|
1285 |
| - """ |
1286 |
| - raise NotImplementedError('use ``sl.add(value)`` instead') |
1287 |
| - |
1288 |
| - |
1289 |
| - def extend(self, values): |
1290 |
| - """Raise not-implemented error. |
1291 |
| -
|
1292 |
| - Implemented to override `MutableSequence.extend` which provides an |
1293 |
| - erroneous default implementation. |
1294 |
| -
|
1295 |
| - :raises NotImplementedError: use ``sl.update(values)`` instead |
1296 |
| -
|
1297 |
| - """ |
1298 |
| - raise NotImplementedError('use ``sl.update(values)`` instead') |
1299 |
| - |
1300 |
| - |
1301 |
| - def insert(self, index, value): |
1302 |
| - """Raise not-implemented error. |
1303 |
| -
|
1304 |
| - :raises NotImplementedError: use ``sl.add(value)`` instead |
1305 |
| -
|
1306 |
| - """ |
1307 |
| - raise NotImplementedError('use ``sl.add(value)`` instead') |
| 1277 | + raise AttributeError(msg) |
1308 | 1278 |
|
1309 | 1279 |
|
1310 | 1280 | def pop(self, index=-1):
|
|
0 commit comments