Skip to content

Commit 801a4ce

Browse files
committed
Improve the hashability of referencing exceptions when they contain hashable data.
Closes: #1126
1 parent 3fb1617 commit 801a4ce

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v4.18.4
2+
=======
3+
4+
* Improve the hashability of wrapped referencing exceptions when they contain hashable data.
5+
16
v4.18.3
27
=======
38

jsonschema/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ def __eq__(self, other):
230230
def __getattr__(self, attr):
231231
return getattr(self._wrapped, attr)
232232

233+
def __hash__(self):
234+
return hash(self._wrapped)
235+
233236
def __repr__(self):
234237
return f"<WrappedReferencingError {self._wrapped!r}>"
235238

jsonschema/tests/test_deprecations.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,24 @@ def test_catching_Unresolvable_via_RefResolutionError(self):
211211
(u.exception, "Unresolvable: urn:nothing")
212212
)
213213

214+
def test_WrappedReferencingError_hashability(self):
215+
"""
216+
Ensure the wrapped referencing errors are hashable when possible.
217+
"""
218+
with self.assertWarns(DeprecationWarning):
219+
from jsonschema import RefResolutionError
220+
221+
validator = validators.Draft202012Validator({"$ref": "urn:nothing"})
222+
223+
with self.assertRaises(referencing.exceptions.Unresolvable) as u:
224+
validator.validate(12)
225+
226+
with self.assertRaises(RefResolutionError) as e:
227+
validator.validate(12)
228+
229+
self.assertIn(e.exception, {u.exception})
230+
self.assertIn(u.exception, {e.exception})
231+
214232
def test_Validator_subclassing(self):
215233
"""
216234
As of v4.12.0, subclassing a validator class produces an explicit

0 commit comments

Comments
 (0)