feat(TextHandler): Make slicing return TextHandlers + autocompletion fixes
and access by index return `TextHandler`
This commit is contained in:
@@ -23,12 +23,25 @@ class TextHandler(str):
|
|||||||
return super().__new__(cls, string)
|
return super().__new__(cls, string)
|
||||||
return super().__new__(cls, '')
|
return super().__new__(cls, '')
|
||||||
|
|
||||||
# Make methods from original `str` class return `TextHandler` instead of returning `str` again
|
@typing.overload
|
||||||
# Of course, I made sonnet write it for me :)
|
def __getitem__(self, key: SupportsIndex) -> 'TextHandler':
|
||||||
def split(self, sep: str = None, maxsplit: SupportsIndex = -1) -> 'TextHandlers[_TextHandlerType]':
|
pass
|
||||||
return TextHandlers([
|
|
||||||
typing.cast("_TextHandlerType", s) for s in super().split(sep, maxsplit)
|
@typing.overload
|
||||||
])
|
def __getitem__(self, key: slice) -> "TextHandlers":
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __getitem__(self, key: Union[SupportsIndex, slice]) -> Union["TextHandler", "TextHandlers"]:
|
||||||
|
lst = super().__getitem__(key)
|
||||||
|
if isinstance(key, slice):
|
||||||
|
lst = [TextHandler(s) for s in lst]
|
||||||
|
return TextHandlers(typing.cast(List[_TextHandlerType], lst))
|
||||||
|
return typing.cast(_TextHandlerType, TextHandler(lst))
|
||||||
|
|
||||||
|
def split(self, sep: str = None, maxsplit: SupportsIndex = -1) -> 'TextHandlers':
|
||||||
|
return TextHandlers(
|
||||||
|
typing.cast(List[_TextHandlerType], [TextHandler(s) for s in super().split(sep, maxsplit)])
|
||||||
|
)
|
||||||
|
|
||||||
def strip(self, chars: str = None) -> Union[str, 'TextHandler']:
|
def strip(self, chars: str = None) -> Union[str, 'TextHandler']:
|
||||||
return TextHandler(super().strip(chars))
|
return TextHandler(super().strip(chars))
|
||||||
@@ -161,18 +174,26 @@ class TextHandler(str):
|
|||||||
return result[0] if result else default
|
return result[0] if result else default
|
||||||
|
|
||||||
|
|
||||||
class TextHandlers(List[_TextHandlerType]):
|
class TextHandlers(List[TextHandler]):
|
||||||
"""
|
"""
|
||||||
The :class:`TextHandlers` class is a subclass of the builtin ``List`` class, which provides a few additional methods.
|
The :class:`TextHandlers` class is a subclass of the builtin ``List`` class, which provides a few additional methods.
|
||||||
"""
|
"""
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
def __getitem__(self, pos: Union[SupportsIndex, slice]) -> Union[TextHandler, "TextHandlers[TextHandler]"]:
|
@typing.overload
|
||||||
|
def __getitem__(self, pos: SupportsIndex) -> TextHandler:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@typing.overload
|
||||||
|
def __getitem__(self, pos: slice) -> "TextHandlers":
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __getitem__(self, pos: Union[SupportsIndex, slice]) -> Union[TextHandler, "TextHandlers"]:
|
||||||
lst = super().__getitem__(pos)
|
lst = super().__getitem__(pos)
|
||||||
if isinstance(pos, slice):
|
if isinstance(pos, slice):
|
||||||
return self.__class__(lst)
|
lst = [TextHandler(s) for s in lst]
|
||||||
else:
|
return TextHandlers(typing.cast(List[_TextHandlerType], lst))
|
||||||
return lst
|
return typing.cast(_TextHandlerType, TextHandler(lst))
|
||||||
|
|
||||||
def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True, clean_match: bool = False,
|
def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True, clean_match: bool = False,
|
||||||
case_sensitive: bool = False) -> 'List[str]':
|
case_sensitive: bool = False) -> 'List[str]':
|
||||||
|
|||||||
Reference in New Issue
Block a user