An immutable version of a dict.
| 108 | # a type checking section and other workaround for the crash |
| 109 | @cython.cclass |
| 110 | class immutabledict(Dict[_KT, _VT]): |
| 111 | """An immutable version of a dict.""" |
| 112 | |
| 113 | # ImmutableDictBase start |
| 114 | @classmethod |
| 115 | def __class_getitem__( # type: ignore[override] |
| 116 | cls, key: Any |
| 117 | ) -> type[Self]: |
| 118 | return cls |
| 119 | |
| 120 | def __delitem__(self, key: Any) -> NoReturn: |
| 121 | _immutable_fn(self) |
| 122 | |
| 123 | def __setitem__(self, key: Any, value: Any) -> NoReturn: |
| 124 | _immutable_fn(self) |
| 125 | |
| 126 | def __setattr__(self, key: Any, value: Any) -> NoReturn: |
| 127 | _immutable_fn(self) |
| 128 | |
| 129 | def clear(self) -> NoReturn: |
| 130 | _immutable_fn(self) |
| 131 | |
| 132 | def pop(self, key: Any, default: Optional[Any] = None) -> NoReturn: |
| 133 | _immutable_fn(self) |
| 134 | |
| 135 | def popitem(self) -> NoReturn: |
| 136 | _immutable_fn(self) |
| 137 | |
| 138 | def setdefault(self, key: Any, default: Optional[Any] = None) -> NoReturn: |
| 139 | _immutable_fn(self) |
| 140 | |
| 141 | def update(self, *arg: Any, **kw: Any) -> NoReturn: |
| 142 | _immutable_fn(self) |
| 143 | |
| 144 | # ImmutableDictBase end |
| 145 | |
| 146 | def __repr__(self) -> str: |
| 147 | return f"immutabledict({dict.__repr__(self)})" |
| 148 | |
| 149 | @cython.annotation_typing(False) # avoid cython crash from generic return |
| 150 | def union( |
| 151 | self, *dicts: Optional[Mapping[_KT, _VT]] |
| 152 | ) -> immutabledict[_KT, _VT]: |
| 153 | return self._union_other(dicts) # type: ignore[no-any-return] |
| 154 | |
| 155 | @cython.annotation_typing(False) # avoid cython crash from generic return |
| 156 | def merge_with( |
| 157 | self, *dicts: Optional[Mapping[_KT, _VT]] |
| 158 | ) -> immutabledict[_KT, _VT]: |
| 159 | # this is an alias of union |
| 160 | return self._union_other(dicts) # type: ignore[no-any-return] |
| 161 | |
| 162 | @cython.cfunc |
| 163 | @cython.inline |
| 164 | def _union_other(self, others: tuple) -> immutabledict: |
| 165 | size = len(others) |
| 166 | if size == 0: |
| 167 | return self |
no outgoing calls