From 55e3fbf0d65921a87421ddb8eea57cc6397f0ea5 Mon Sep 17 00:00:00 2001 From: AW Date: Fri, 29 May 2026 15:14:22 +0000 Subject: [PATCH] fix(exceptions): prevent ErrorTree.__getitem__ from mutating _contents --- jsonschema/exceptions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 2e5d4ca0..cf005680 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -326,7 +326,7 @@ def __init__(self, errors: Iterable[ValidationError] = ()): for error in errors: container = self for element in error.path: - container = container[element] + container = container._contents[element] container.errors[error.validator] = error container._instance = error.instance @@ -348,6 +348,8 @@ def __getitem__(self, index): """ if self._instance is not _unset and index not in self: self._instance[index] + if index not in self._contents: + return self.__class__() return self._contents[index] def __setitem__(self, index: str | int, value: ErrorTree):