From 801a4cecc7a0cf3653db8e054fee5e800cd471e5 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Tue, 18 Jul 2023 10:23:04 +0200 Subject: [PATCH] Improve the hashability of referencing exceptions when they contain hashable data. Closes: #1126 --- CHANGELOG.rst | 5 +++++ jsonschema/exceptions.py | 3 +++ jsonschema/tests/test_deprecations.py | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index be6d73b71..84df7c661 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 80679c1b5..80281057e 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 7c287427b..85927e695 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