mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Add (deep)copy support to read only dict (#118204)
This commit is contained in:
parent
56431ef750
commit
9dc580e5de
@ -1,5 +1,6 @@
|
|||||||
"""Read only dictionary."""
|
"""Read only dictionary."""
|
||||||
|
|
||||||
|
from copy import deepcopy
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
@ -18,3 +19,13 @@ class ReadOnlyDict[_KT, _VT](dict[_KT, _VT]):
|
|||||||
clear = _readonly
|
clear = _readonly
|
||||||
update = _readonly
|
update = _readonly
|
||||||
setdefault = _readonly
|
setdefault = _readonly
|
||||||
|
|
||||||
|
def __copy__(self) -> dict[_KT, _VT]:
|
||||||
|
"""Create a shallow copy."""
|
||||||
|
return ReadOnlyDict(self)
|
||||||
|
|
||||||
|
def __deepcopy__(self, memo: Any) -> dict[_KT, _VT]:
|
||||||
|
"""Create a deep copy."""
|
||||||
|
return ReadOnlyDict(
|
||||||
|
{deepcopy(key, memo): deepcopy(value, memo) for key, value in self.items()}
|
||||||
|
)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""Test read only dictionary."""
|
"""Test read only dictionary."""
|
||||||
|
|
||||||
|
import copy
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -35,3 +36,5 @@ def test_read_only_dict() -> None:
|
|||||||
assert isinstance(data, dict)
|
assert isinstance(data, dict)
|
||||||
assert dict(data) == {"hello": "world"}
|
assert dict(data) == {"hello": "world"}
|
||||||
assert json.dumps(data) == json.dumps({"hello": "world"})
|
assert json.dumps(data) == json.dumps({"hello": "world"})
|
||||||
|
|
||||||
|
assert copy.deepcopy(data) == {"hello": "world"}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user