make exceptions repr tests Python 3.7-friendly by eval-repr round-trip
+def exceptions_eq(e1, e2):
+ return type(e1) is type(e2) and e1.args == e2.args
+
+def round_trip_repr(e):
+ assert exceptions_eq(e, eval(repr(e)))
+
# KeyError
empty_exc = KeyError()
assert str(empty_exc) == ''
-assert repr(empty_exc) == 'KeyError()'
+round_trip_repr(empty_exc)
assert len(empty_exc.args) == 0
assert type(empty_exc.args) == tuple
exc = KeyError('message')
assert str(exc) == "'message'"
-assert repr(exc) == "KeyError('message',)"
+round_trip_repr(exc)
exc = KeyError('message', 'another message')
assert str(exc) == "('message', 'another message')"
-assert repr(exc) == "KeyError('message', 'another message')"
+round_trip_repr(exc)
assert exc.args[0] == 'message'
assert exc.args[1] == 'another message'
class A:
def __repr__(self):
- return 'repr'
+ return 'A()'
def __str__(self):
return 'str'
+ def __eq__(self, other):
+ return type(other) is A
exc = KeyError(A())
-assert str(exc) == 'repr'
-assert repr(exc) == 'KeyError(repr,)'
+assert str(exc) == 'A()'
+round_trip_repr(exc)
# ImportError / ModuleNotFoundError
exc = ImportError()
