Prevent in-place union from modifying ReadOnlyDict (#187625)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Raman Gupta
2026-10-20 20:17:40 -04:00
committed by GitHub
co-authored by Claude Opus 5.5
parent cbbd58b4ca
commit 19253b0418
2 changed files with 4 additions and 0 deletions
+1
View File
@@ -20,6 +20,7 @@ class ReadOnlyDict[_KT, _VT](dict[_KT, _VT]):
clear = _readonly
update = _readonly
setdefault = _readonly
__ior__ = _readonly
def __copy__(self) -> dict[_KT, _VT]:
"""Create a shallow copy."""
+3
View File
@@ -33,6 +33,9 @@ def test_read_only_dict() -> None:
with pytest.raises(RuntimeError):
data.setdefault("yo", "yo")
with pytest.raises(RuntimeError):
data |= {"yo": "yo"}
assert isinstance(data, dict)
assert dict(data) == {"hello": "world"}
assert json.dumps(data) == json.dumps({"hello": "world"})