Open
Description
The following example illustrate that accessing of an index in an ErrorTree
object that has no error can added the index to the collection of indices considered having errors.
from jsonschema import Draft202012Validator
from jsonschema.exceptions import ErrorTree
schema = {
"type" : "array",
"items" : {"type" : "number", "enum" : [1, 2, 3]},
"minItems" : 3,
}
instance = ["spam", 2]
v = Draft202012Validator(schema)
for error in sorted(v.iter_errors(instance), key=str):
print(error.message)
print("\n====================================")
tree = ErrorTree(v.iter_errors(instance))
print(f"correct value of list(tree): {list(tree)}")
"correct value of list(tree): [0]"
print(f"correct value of `1 in tree`: {1 in tree}")
"correct value of `1 in tree`: False"
# Accessing the 1 index changes the iteration of the tree
tree[1]
print("\n ===== after accessing the 1 index of the tree =====")
print(f"incorrect value of list(tree): {list(tree)}")
"incorrect value of list(tree): [0, 1]"
print(f"incorrect value of `1 in tree`: {1 in tree}")
"incorrect value of `1 in tree`: True"
This behavior contradicts with the documentation at https://python-jsonschema.readthedocs.io/en/latest/errors/#errortrees and is a bug.