33
33
# stdlib
34
34
from contextlib import contextmanager
35
35
from itertools import chain
36
- from typing import Any , Iterable , Iterator , List , Tuple , TypeVar , Union , cast , overload
36
+ from typing import Any , Iterable , Iterator , List , Reversible , Tuple , TypeVar , Union , cast , overload
37
37
38
38
# this package
39
39
from domdf_python_tools .doctools import prettify_docstrings
40
- from domdf_python_tools .typing import String
40
+ from domdf_python_tools .typing import String , SupportsIndex
41
41
from domdf_python_tools .utils import convert_indents
42
42
43
43
__all__ = ["Indent" , "StringList" , "DelimitedList" , "_SL" , "splitlines" , "joinlines" ]
@@ -202,16 +202,20 @@ def count_blanklines(self) -> int:
202
202
203
203
return self .count ('' )
204
204
205
- def insert (self , index : int , line : String ) -> None :
205
+ def insert (self , index : SupportsIndex , line : String ) -> None :
206
206
"""
207
207
Insert a line into the :class:`~domdf_python_tools.stringlist.StringList` at the given position.
208
208
209
209
:param index:
210
210
:param line:
211
+
212
+ .. versionchanged:: 3.2.0 Changed :class:`int` in the type annotation to :protocol:`~.SupportsIndex`.
211
213
"""
212
214
213
215
lines : List [str ]
214
216
217
+ index = index .__index__ ()
218
+
215
219
if index < 0 or index > len (self ):
216
220
lines = str (line ).split ('\n ' )
217
221
else :
@@ -221,12 +225,12 @@ def insert(self, index: int, line: String) -> None:
221
225
super ().insert (index , self ._make_line (inner_line ))
222
226
223
227
@overload
224
- def __setitem__ (self , index : int , line : String ) -> None : ...
228
+ def __setitem__ (self , index : SupportsIndex , line : String ) -> None : ...
225
229
226
230
@overload
227
231
def __setitem__ (self , index : slice , line : Iterable [String ]) -> None : ...
228
232
229
- def __setitem__ (self , index : Union [int , slice ], line : Union [String , Iterable [String ]]):
233
+ def __setitem__ (self , index : Union [SupportsIndex , slice ], line : Union [String , Iterable [String ]]):
230
234
"""
231
235
Replaces the given line with new content.
232
236
@@ -235,29 +239,39 @@ def __setitem__(self, index: Union[int, slice], line: Union[String, Iterable[Str
235
239
236
240
:param index:
237
241
:param line:
242
+
243
+ .. versionchanged:: 3.2.0 Changed :class:`int` in the type annotation to :protocol:`~.SupportsIndex`.
238
244
"""
239
245
240
- if isinstance (index , int ):
246
+ if isinstance (index , slice ):
247
+ line = cast (Iterable [String ], line )
248
+
249
+ if not isinstance (line , Reversible ):
250
+ line = tuple (line )
251
+
252
+ for lline , idx in zip (
253
+ reversed (line ),
254
+ reversed (range (index .start or 0 , index .stop + 1 , index .step or 1 )),
255
+ ):
256
+ self [idx ] = lline
257
+ else :
258
+ line = cast (String , line )
259
+ index = index .__index__ ()
260
+
241
261
if self and index < len (self ):
242
262
self .pop (index )
243
263
if index < 0 :
244
264
index = len (self ) + index + 1
245
- self .insert (index , line )
246
265
247
- elif isinstance (index , slice ):
248
- for line , idx in zip ( # pylint: disable=redefined-argument-from-local
249
- reversed (line ), # type: ignore
250
- reversed (range (index .start or 0 , index .stop + 1 , index .step or 1 )),
251
- ):
252
- self [idx ] = line
266
+ self .insert (index , line )
253
267
254
268
@overload
255
- def __getitem__ (self , index : int ) -> str : ...
269
+ def __getitem__ (self , index : SupportsIndex ) -> str : ...
256
270
257
271
@overload
258
272
def __getitem__ (self : _SL , index : slice ) -> _SL : ...
259
273
260
- def __getitem__ (self : _SL , index : Union [int , slice ]) -> Union [str , _SL ]:
274
+ def __getitem__ (self : _SL , index : Union [SupportsIndex , slice ]) -> Union [str , _SL ]:
261
275
r"""
262
276
Returns the line with the given index.
263
277
@@ -268,6 +282,8 @@ def __getitem__(self: _SL, index: Union[int, slice]) -> Union[str, _SL]:
268
282
.. versionchanged:: 1.8.0
269
283
270
284
Now returns a :class:`~domdf_python_tools.stringlist.StringList` when ``index`` is a :class:`slice`.
285
+
286
+ .. versionchanged:: 3.2.0 Changed :class:`int` in the type annotation to :protocol:`~.SupportsIndex`.
271
287
"""
272
288
273
289
if isinstance (index , slice ):
0 commit comments