Skip to content

Commit 3b486f7

Browse files
committed
Deprecate ErrorTree.__setitem__.
It exposes mutating a jsonschema type unnecessarily, and it's unclear it's actually working even, as it doesn't properly keep .errors up to date as well. It will be removed in a future version.
1 parent 486cf83 commit 3b486f7

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v4.20.0
2+
=======
3+
4+
* ``jsonschema.exceptions.ErrorTree.__setitem__`` is now deprecated.
5+
More broadly, in general users of ``jsonschema`` should never be mutating objects owned by the library.
6+
17
v4.19.2
28
=======
39

jsonschema/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,18 @@ def __getitem__(self, index):
331331
def __setitem__(self, index, value):
332332
"""
333333
Add an error to the tree at the given ``index``.
334+
335+
.. deprecated:: v4.20.0
336+
337+
Setting items on an `ErrorTree` is deprecated without replacement.
338+
To populate a tree, provide all of its suberrors when you construct
339+
the tree.
334340
"""
341+
warnings.warn(
342+
"ErrorTree.__setitem__ is deprecated without replacement.",
343+
DeprecationWarning,
344+
stacklevel=2,
345+
)
335346
self._contents[index] = value
336347

337348
def __iter__(self):

jsonschema/tests/test_deprecations.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ def test_import_ErrorTree(self):
5151
self.assertEqual(ErrorTree, exceptions.ErrorTree)
5252
self.assertEqual(w.filename, __file__)
5353

54+
def test_ErrorTree_setitem(self):
55+
"""
56+
As of v4.20.0, setting items on an ErrorTree is deprecated.
57+
"""
58+
59+
e = exceptions.ValidationError("some error", path=["foo"])
60+
tree = exceptions.ErrorTree()
61+
subtree = exceptions.ErrorTree(errors=[e])
62+
63+
message = "ErrorTree.__setitem__ is "
64+
with self.assertWarnsRegex(DeprecationWarning, message) as w:
65+
tree["foo"] = subtree
66+
67+
self.assertEqual(tree["foo"], subtree)
68+
self.assertEqual(w.filename, __file__)
69+
5470
def test_import_FormatError(self):
5571
"""
5672
As of v4.18.0, importing FormatError from the package root is

0 commit comments

Comments
 (0)