diff --git a/CHANGELOG.rst b/CHANGELOG.rst index be6d73b7..84df7c66 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +v4.18.4 +======= + +* Improve the hashability of wrapped referencing exceptions when they contain hashable data. + v4.18.3 ======= diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 80679c1b..80281057 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -230,6 +230,9 @@ def __eq__(self, other): def __getattr__(self, attr): return getattr(self._wrapped, attr) + def __hash__(self): + return hash(self._wrapped) + def __repr__(self): return f"" diff --git a/jsonschema/tests/test_deprecations.py b/jsonschema/tests/test_deprecations.py index 7c287427..85927e69 100644 --- a/jsonschema/tests/test_deprecations.py +++ b/jsonschema/tests/test_deprecations.py @@ -211,6 +211,24 @@ def test_catching_Unresolvable_via_RefResolutionError(self): (u.exception, "Unresolvable: urn:nothing") ) + def test_WrappedReferencingError_hashability(self): + """ + Ensure the wrapped referencing errors are hashable when possible. + """ + with self.assertWarns(DeprecationWarning): + from jsonschema import RefResolutionError + + validator = validators.Draft202012Validator({"$ref": "urn:nothing"}) + + with self.assertRaises(referencing.exceptions.Unresolvable) as u: + validator.validate(12) + + with self.assertRaises(RefResolutionError) as e: + validator.validate(12) + + self.assertIn(e.exception, {u.exception}) + self.assertIn(u.exception, {e.exception}) + def test_Validator_subclassing(self): """ As of v4.12.0, subclassing a validator class produces an explicit