Skip to content

Commit 9fee59e

Browse files
committed
make exceptions repr tests Python 3.7-friendly by eval-repr round-trip
1 parent cc24b73 commit 9fee59e

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

tests/snippets/exceptions.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1+
def exceptions_eq(e1, e2):
2+
return type(e1) is type(e2) and e1.args == e2.args
3+
4+
def round_trip_repr(e):
5+
assert exceptions_eq(e, eval(repr(e)))
6+
17
# KeyError
28
empty_exc = KeyError()
39
assert str(empty_exc) == ''
4-
assert repr(empty_exc) == 'KeyError()'
10+
round_trip_repr(empty_exc)
511
assert len(empty_exc.args) == 0
612
assert type(empty_exc.args) == tuple
713

814
exc = KeyError('message')
915
assert str(exc) == "'message'"
10-
assert repr(exc) == "KeyError('message',)"
16+
round_trip_repr(exc)
1117

1218
exc = KeyError('message', 'another message')
1319
assert str(exc) == "('message', 'another message')"
14-
assert repr(exc) == "KeyError('message', 'another message')"
20+
round_trip_repr(exc)
1521
assert exc.args[0] == 'message'
1622
assert exc.args[1] == 'another message'
1723

1824
class A:
1925
def __repr__(self):
20-
return 'repr'
26+
return 'A()'
2127
def __str__(self):
2228
return 'str'
29+
def __eq__(self, other):
30+
return type(other) is A
2331

2432
exc = KeyError(A())
25-
assert str(exc) == 'repr'
26-
assert repr(exc) == 'KeyError(repr,)'
33+
assert str(exc) == 'A()'
34+
round_trip_repr(exc)
2735

2836
# ImportError / ModuleNotFoundError
2937
exc = ImportError()

0 commit comments

Comments
 (0)