Problem
Passing an iterator as ValidationError(context=...) retains its children, but does not set their parent. The iterator is consumed by self.context = list(context), then iterated again when assigning parents. This also affects SchemaError.
from jsonschema.exceptions import ValidationError
child = ValidationError("child", path=["name"], schema_path=[0, "type"])
parent = ValidationError(
"parent",
path=["record"],
schema_path=["properties", "record", "anyOf"],
context=iter([child]),
)
print(parent.context == [child]) # True
print(child.parent is parent) # False (expected True)
print(child.json_path) # $.name (expected $.record.name)
Lists and tuples assign the parent correctly. Setting parents from the stored self.context instead of the original iterable should preserve the same behavior for one-shot iterators.
Reproduced on main (51cd75e39), macOS, Python 3.12.13. A regression test fails for iterator contexts with both error classes and passes for list/tuple contexts.
Investigated with Codex assistance.
Problem
Passing an iterator as
ValidationError(context=...)retains its children, but does not set theirparent. The iterator is consumed byself.context = list(context), then iterated again when assigning parents. This also affectsSchemaError.Lists and tuples assign the parent correctly. Setting parents from the stored
self.contextinstead of the original iterable should preserve the same behavior for one-shot iterators.Reproduced on main (
51cd75e39), macOS, Python 3.12.13. A regression test fails for iterator contexts with both error classes and passes for list/tuple contexts.Investigated with Codex assistance.