Skip to content

Commit 0093d61

Browse files
committed
Make __getitem__ of StringList return a Stringlist for slices.
1 parent 4f3fb34 commit 0093d61

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

domdf_python_tools/stringlist.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
__all__ = ["Indent", "StringList", "DelimitedList"]
4040

4141
_S = TypeVar("_S")
42+
_SL = TypeVar("_SL", bound="StringList")
4243

4344

4445
@prettify_docstrings
@@ -172,11 +173,13 @@ def extend(self, iterable: Iterable[String]) -> None:
172173
for line in iterable:
173174
self.append(line)
174175

175-
def copy(self) -> "StringList":
176+
def copy(self: _SL) -> _SL:
176177
"""
177178
Returns a shallow copy of the :class:`~domdf_python_tools.stringlist.StringList`.
178179
179180
Equivalent to ``a[:]``.
181+
182+
:rtype: :class:`~domdf_python_tools.stringlist.StringList`
180183
"""
181184

182185
return self.__class__(super().copy())
@@ -246,17 +249,26 @@ def __getitem__(self, index: int) -> str:
246249
... # pragma: no cover
247250

248251
@overload
249-
def __getitem__(self, index: slice) -> List[str]:
252+
def __getitem__(self: _SL, index: slice) -> _SL:
250253
... # pragma: no cover
251254

252-
def __getitem__(self, index: Union[int, slice]) -> Union[str, List[str]]:
253-
"""
255+
def __getitem__(self: _SL, index: Union[int, slice]) -> Union[str, _SL]:
256+
r"""
254257
Returns the line with the given index.
255258
256259
:param index:
260+
261+
:rtype: :py:obj:`~typing.Union`\[:class:`str`, :class:`~domdf_python_tools.stringlist.StringList`\]
262+
263+
.. versionchanged:: 1.8.0
264+
265+
Now returns a :class:`~domdf_python_tools.stringlist.StringList` when ``index`` is a :class:`slice`.
257266
"""
258267

259-
return super().__getitem__(index)
268+
if isinstance(index, slice):
269+
return self.__class__(super().__getitem__(index))
270+
else:
271+
return super().__getitem__(index)
260272

261273
def blankline(self, ensure_single: bool = False):
262274
"""

0 commit comments

Comments
 (0)