jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

Fix ErrorTree polluting contents on index access

Open bysiber opened this issue 9 months ago • 1 comments

Fixes #1328.

ErrorTree uses a defaultdict(self.__class__) for _contents, which means any __getitem__ access on a missing key silently creates an empty entry. After that, __iter__ and __contains__ return incorrect results because they see the auto-created entry as if it had errors.

The fix replaces defaultdict with a regular dict. The constructor now explicitly creates subtrees when populating the tree from errors, and __getitem__ returns a new empty ErrorTree for valid indices that aren't in _contents without storing it. This preserves the current behavior where you can still access error-free indices and inspect the result, but without polluting the tree state.

Added a test that verifies list(tree) and in checks are unaffected after accessing an index with no errors.

bysiber avatar Feb 21 '26 23:02 bysiber

Documentation build overview

📚 python-jsonschema | 🛠️ Build #31506925 | 📁 Comparing 23c8c8b979690a57fcab84ed775e1609409ef7fd against latest (5a5fa97c7d5409b9f1f1280e51a3e63334b105ae)


🔍 Preview build

Show files changed (1 files in total): 📝 1 modified | ➕ 0 added | ➖ 0 deleted
File Status
_modules/jsonschema/exceptions/index.html 📝 modified