Skip to content

fix(exceptions): prevent ErrorTree.__getitem__ from mutating _contents#1490

Draft
b-wulan wants to merge 1 commit into
python-jsonschema:mainfrom
b-wulan:fix/2026-05-29-errortree-getitem-mutation
Draft

fix(exceptions): prevent ErrorTree.__getitem__ from mutating _contents#1490
b-wulan wants to merge 1 commit into
python-jsonschema:mainfrom
b-wulan:fix/2026-05-29-errortree-getitem-mutation

Conversation

@b-wulan
Copy link
Copy Markdown

@b-wulan b-wulan commented May 29, 2026

Accessing tree[index] on an index with no errors was silently inserting an empty ErrorTree into _contents via defaultdict, causing subsequent __iter__ and __contains__ calls to report phantom entries (fixes #1328).

The fix has two parts: __init__ now navigates via _contents[element] directly (bypassing __getitem__) to build the tree, and __getitem__ returns a fresh detached empty tree when the index is absent rather than triggering the defaultdict factory.

To verify: construct a tree with errors only at index 0, access tree[1], then confirm list(tree) == [0] and 1 not in tree.

@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 python-jsonschema | 🛠️ Build #32905817 | 📁 Comparing 55e3fbf against latest (9d05948)

  🔍 Preview build  

1 file changed
± _modules/jsonschema/exceptions/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant