Skip to content

The return of __iter__() and __contains__() change after accessing of an index with no error #1328

Open
@candleindark

Description

@candleindark

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething doesn't work the way it should.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions