Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions tests/snippets/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
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)))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicky, but maybe this should return and the assert should be where it's called? That way an error would show the line with the exception variable instead of just the parameters.

Copy link
Copy Markdown
Contributor Author

@silmeth silmeth Jul 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I based it on the round_trip_test() from json_snippet.py where it has similar logic. But I agree it’d be a bit nicer to move asserts out of the function. Shall I change it just here, or in both snippets (I haven’t found any other round-tripy utils)?


# 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()
Expand Down