# SPDX-License-Identifier: GPLv3-or-later
# Copyright © 2025 pygaindalf Rui Pinheiro
from abc import ABCMeta
from collections.abc import Iterable, MutableSequence, Sequence
from typing import cast as typing_cast
from typing import overload, override
from .collection import ProxyCollection, ProxyMutableCollection
from .iterable import ProxyIterable
[docs]
class ProxySequence[
T_Item: object,
T_Proxy: object,
](ProxyIterable[T_Item, T_Proxy, Sequence[T_Item]], ProxyCollection[T_Item, T_Proxy, Sequence[T_Item]], Sequence[T_Proxy], metaclass=ABCMeta):
@overload
def __getitem__(self, index: int) -> T_Proxy: ...
@overload
def __getitem__(self, index: slice) -> MutableSequence[T_Proxy]: ...
@override
def __getitem__(self, index: int | slice) -> T_Proxy | MutableSequence[T_Proxy]:
if isinstance(index, slice):
msg = "Sliced read access not implemented yet"
raise NotImplementedError(msg)
item = self._get_field()[index]
return self._convert_item_to_proxy(item)
[docs]
class ProxyMutableSequence[
T_Item: object,
T_Proxy: object,
](
ProxySequence[T_Item, T_Proxy],
ProxyMutableCollection[T_Item, T_Proxy, Sequence[T_Item], MutableSequence[T_Item]],
MutableSequence[T_Proxy],
metaclass=ABCMeta,
):
@overload
def __setitem__(self, index: int, value: T_Proxy) -> None: ...
@overload
def __setitem__(self, index: slice, value: Iterable[T_Proxy]) -> None: ...
@override
def __setitem__(self, index: int | slice, value: T_Proxy | Iterable[T_Proxy]) -> None:
if isinstance(index, slice):
msg = "Sliced write access not implemented yet"
raise NotImplementedError(msg)
value = typing_cast("T_Proxy", value) # since we checked it's not a slice
item = self._convert_proxy_to_item(value)
self._get_mut_field()[index] = item
@overload
def __delitem__(self, index: int) -> None: ...
@overload
def __delitem__(self, index: slice) -> None: ...
@override
def __delitem__(self, index: int | slice) -> None:
del self._get_mut_field()[index]
__getitem__ = ProxySequence.__getitem__ # pyright: ignore[reportAssignmentType]
[docs]
@override
def clear(self) -> None:
self._get_mut_field().clear()
[docs]
@override
def insert(self, index: int, value: T_Proxy) -> None:
item = self._convert_proxy_to_item(value)
self._get_mut_field().insert(index, item)