Home Assistant Unofficial Reference 2024.12.1
read_only_dict.py
Go to the documentation of this file.
1 """Read only dictionary."""
2 
3 from copy import deepcopy
4 from typing import Any
5 
6 
7 def _readonly(*args: Any, **kwargs: Any) -> Any:
8  """Raise an exception when a read only dict is modified."""
9  raise RuntimeError("Cannot modify ReadOnlyDict")
10 
11 
12 class ReadOnlyDict[_KT, _VT](dict[_KT, _VT]):
13  """Read only version of dict that is compatible with dict types."""
14 
15  __setitem__ = _readonly
16  __delitem__ = _readonly
17  pop = _readonly
18  popitem = _readonly
19  clear = _readonly
20  update = _readonly
21  setdefault = _readonly
22 
23  def __copy__(self) -> dict[_KT, _VT]:
24  """Create a shallow copy."""
25  return ReadOnlyDict(self)
26 
27  def __deepcopy__(self, memo: Any) -> dict[_KT, _VT]:
28  """Create a deep copy."""
29  return ReadOnlyDict(
30  {deepcopy(key, memo): deepcopy(value, memo) for key, value in self.items()}
31  )
dict[_KT, _VT] __deepcopy__(self, Any memo)
Any _readonly(*Any args, **Any kwargs)