|
39 | 39 | __all__ = ["Indent", "StringList", "DelimitedList"]
|
40 | 40 |
|
41 | 41 | _S = TypeVar("_S")
|
| 42 | +_SL = TypeVar("_SL", bound="StringList") |
42 | 43 |
|
43 | 44 |
|
44 | 45 | @prettify_docstrings
|
@@ -172,11 +173,13 @@ def extend(self, iterable: Iterable[String]) -> None:
|
172 | 173 | for line in iterable:
|
173 | 174 | self.append(line)
|
174 | 175 |
|
175 |
| - def copy(self) -> "StringList": |
| 176 | + def copy(self: _SL) -> _SL: |
176 | 177 | """
|
177 | 178 | Returns a shallow copy of the :class:`~domdf_python_tools.stringlist.StringList`.
|
178 | 179 |
|
179 | 180 | Equivalent to ``a[:]``.
|
| 181 | +
|
| 182 | + :rtype: :class:`~domdf_python_tools.stringlist.StringList` |
180 | 183 | """
|
181 | 184 |
|
182 | 185 | return self.__class__(super().copy())
|
@@ -246,17 +249,26 @@ def __getitem__(self, index: int) -> str:
|
246 | 249 | ... # pragma: no cover
|
247 | 250 |
|
248 | 251 | @overload
|
249 |
| - def __getitem__(self, index: slice) -> List[str]: |
| 252 | + def __getitem__(self: _SL, index: slice) -> _SL: |
250 | 253 | ... # pragma: no cover
|
251 | 254 |
|
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""" |
254 | 257 | Returns the line with the given index.
|
255 | 258 |
|
256 | 259 | :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`. |
257 | 266 | """
|
258 | 267 |
|
259 |
| - return super().__getitem__(index) |
| 268 | + if isinstance(index, slice): |
| 269 | + return self.__class__(super().__getitem__(index)) |
| 270 | + else: |
| 271 | + return super().__getitem__(index) |
260 | 272 |
|
261 | 273 | def blankline(self, ensure_single: bool = False):
|
262 | 274 | """
|
|
0 commit comments