Skip to content

Immutable list

immutable_list

IList

Bases: list[_T]

A list that can be frozen. Once frozen, it can not be modified. In comparison to FrozenList this supports pattern matching.

Source code in xdsl/utils/immutable_list.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class IList(list[_T]):
    """
    A list that can be frozen. Once frozen, it can not be modified.
    In comparison to FrozenList this supports pattern matching.
    """

    _frozen: bool = False

    def freeze(self):
        self._frozen = True

    def _unfreeze(self):
        self._frozen = False

    def append(self, object: _T, /) -> None:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        return super().append(object)

    def extend(self, iterable: Iterable[_T], /) -> None:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        return super().extend(iterable)

    def insert(self, index: SupportsIndex, object: _T, /) -> None:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        return super().insert(index, object)

    def remove(self, value: _T, /) -> None:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        return super().remove(value)

    def pop(self, index: SupportsIndex = -1, /) -> _T:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        return super().pop(index)

    def clear(self) -> None:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        return super().clear()

    @overload
    def __setitem__(self, key: SupportsIndex, value: _T) -> None: ...

    @overload
    def __setitem__(self, key: slice, value: Iterable[_T]) -> None: ...

    def __setitem__(self, key: SupportsIndex | slice, value: _T | Iterable[_T]) -> None:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        if isinstance(key, slice):
            o = cast(list[_T], value)
            return super().__setitem__(key, o)
        else:
            o = cast(_T, value)
            return super().__setitem__(key, o)

    def __delitem__(self, key: SupportsIndex | slice, /) -> None:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        return super().__delitem__(key)

    def __add__(self, value: Iterable[_S]) -> IList[_T | _S]:
        return IList(super().__add__(list(value)))

    def __iadd__(self, value: Iterable[_T], /) -> Self:
        if self._frozen:
            raise Exception("frozen list can not be modified")
        return super().__iadd__(value)

    def __eq__(self, value: object, /) -> bool:
        if isinstance(value, IList):
            other = cast(IList[Any], value)
            return super().__eq__(other)
        return False

freeze()

Source code in xdsl/utils/immutable_list.py
20
21
def freeze(self):
    self._frozen = True

append(object: _T) -> None

Source code in xdsl/utils/immutable_list.py
26
27
28
29
def append(self, object: _T, /) -> None:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    return super().append(object)

extend(iterable: Iterable[_T]) -> None

Source code in xdsl/utils/immutable_list.py
31
32
33
34
def extend(self, iterable: Iterable[_T], /) -> None:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    return super().extend(iterable)

insert(index: SupportsIndex, object: _T) -> None

Source code in xdsl/utils/immutable_list.py
36
37
38
39
def insert(self, index: SupportsIndex, object: _T, /) -> None:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    return super().insert(index, object)

remove(value: _T) -> None

Source code in xdsl/utils/immutable_list.py
41
42
43
44
def remove(self, value: _T, /) -> None:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    return super().remove(value)

pop(index: SupportsIndex = -1) -> _T

Source code in xdsl/utils/immutable_list.py
46
47
48
49
def pop(self, index: SupportsIndex = -1, /) -> _T:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    return super().pop(index)

clear() -> None

Source code in xdsl/utils/immutable_list.py
51
52
53
54
def clear(self) -> None:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    return super().clear()

__setitem__(key: SupportsIndex | slice, value: _T | Iterable[_T]) -> None

__setitem__(key: SupportsIndex, value: _T) -> None
__setitem__(key: slice, value: Iterable[_T]) -> None
Source code in xdsl/utils/immutable_list.py
62
63
64
65
66
67
68
69
70
def __setitem__(self, key: SupportsIndex | slice, value: _T | Iterable[_T]) -> None:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    if isinstance(key, slice):
        o = cast(list[_T], value)
        return super().__setitem__(key, o)
    else:
        o = cast(_T, value)
        return super().__setitem__(key, o)

__delitem__(key: SupportsIndex | slice) -> None

Source code in xdsl/utils/immutable_list.py
72
73
74
75
def __delitem__(self, key: SupportsIndex | slice, /) -> None:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    return super().__delitem__(key)

__add__(value: Iterable[_S]) -> IList[_T | _S]

Source code in xdsl/utils/immutable_list.py
77
78
def __add__(self, value: Iterable[_S]) -> IList[_T | _S]:
    return IList(super().__add__(list(value)))

__iadd__(value: Iterable[_T]) -> Self

Source code in xdsl/utils/immutable_list.py
80
81
82
83
def __iadd__(self, value: Iterable[_T], /) -> Self:
    if self._frozen:
        raise Exception("frozen list can not be modified")
    return super().__iadd__(value)

__eq__(value: object) -> bool

Source code in xdsl/utils/immutable_list.py
85
86
87
88
89
def __eq__(self, value: object, /) -> bool:
    if isinstance(value, IList):
        other = cast(IList[Any], value)
        return super().__eq__(other)
    return False