Skip to content

Commit ecb27ef

Browse files
committed
Fix the rendering of __str__ on wrapped referencing exceptions.
Be a bit less heavy handed, using __getattr__ rather than taking over attribute lookups entirely.
1 parent 4916e2e commit ecb27ef

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

jsonschema/exceptions.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,23 @@ def __str__(self):
213213

214214
class _WrappedReferencingError(_RefResolutionError, _Unresolvable):
215215
def __init__(self, cause: _Unresolvable):
216-
object.__setattr__(self, "_cause", cause)
216+
object.__setattr__(self, "_wrapped", cause)
217217

218-
def __getattribute__(self, attr):
219-
cause = object.__getattribute__(self, "_cause")
220-
return getattr(cause, attr)
218+
def __eq__(self, other):
219+
if other.__class__ is self.__class__:
220+
return self._wrapped == other._wrapped
221+
elif other.__class__ is self._wrapped.__class__:
222+
return self._wrapped == other
223+
return NotImplemented
224+
225+
def __getattr__(self, attr):
226+
return getattr(self._wrapped, attr)
227+
228+
def __repr__(self):
229+
return f"<WrappedReferencingError {self._wrapped!r}>"
230+
231+
def __str__(self):
232+
return f"{self._wrapped.__class__.__name__}: {self._wrapped}"
221233

222234

223235
class UndefinedTypeCheck(Exception):

jsonschema/tests/test_deprecations.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ def test_catching_Unresolvable_directly(self):
184184
validator.validate(12)
185185

186186
expected = referencing.exceptions.Unresolvable(ref="urn:nothing")
187-
self.assertEqual(e.exception, expected)
187+
self.assertEqual(
188+
(e.exception, str(e.exception)),
189+
(expected, "Unresolvable: urn:nothing")
190+
)
188191

189192
def test_catching_Unresolvable_via_RefResolutionError(self):
190193
"""
@@ -197,12 +200,17 @@ def test_catching_Unresolvable_via_RefResolutionError(self):
197200

198201
validator = validators.Draft202012Validator({"$ref": "urn:nothing"})
199202

200-
with self.assertRaises(referencing.exceptions.Unresolvable):
203+
with self.assertRaises(referencing.exceptions.Unresolvable) as u:
201204
validator.validate(12)
202205

203-
with self.assertRaises(RefResolutionError):
206+
with self.assertRaises(RefResolutionError) as e:
204207
validator.validate(12)
205208

209+
self.assertEqual(
210+
(e.exception, str(e.exception)),
211+
(u.exception, "Unresolvable: urn:nothing")
212+
)
213+
206214
def test_Validator_subclassing(self):
207215
"""
208216
As of v4.12.0, subclassing a validator class produces an explicit

0 commit comments

Comments
 (0)