Skip to content

Commit

Permalink
Merge pull request #9 from duncanmmacleod/init-typerror
Browse files Browse the repository at this point in the history
Improved TypeError messages in LIGOTimeGPS
  • Loading branch information
Duncan Macleod committed Nov 28, 2018
2 parents 2e7143f + c095855 commit 30fb86a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 4 additions & 3 deletions ligotimegps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def __init__(self, seconds, nanoseconds=0):
try:
seconds = str(Decimal(seconds))
except ArithmeticError:
raise TypeError("invalid literal for LIGOTimeGPS(): %s"
% seconds)
raise TypeError("invalid literal for {0}: {1}".format(
type(self).__name__, seconds))
sign = -1 if seconds.startswith("-") else +1
if "." in seconds:
seconds, ns = seconds.split(".")
Expand All @@ -99,7 +99,8 @@ def __init__(self, seconds, nanoseconds=0):
nanoseconds += seconds.gpsNanoSeconds
seconds = seconds.gpsSeconds
else:
raise TypeError(seconds)
raise TypeError("cannot convert {0!r} ({0.__class__.__name__})"
" to {1.__name__}".format(seconds, type(self)))
self._seconds = seconds + int(nanoseconds // 1000000000)
self._nanoseconds = int(nanoseconds % 1000000000)

Expand Down
14 changes: 9 additions & 5 deletions ligotimegps/test_ligotimegps.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ def test_creation():
assert a.gpsSeconds == 1234567898
assert a.gpsNanoSeconds == 765432100

# check errors
with pytest.raises(TypeError):
LIGOTimeGPS('test')
with pytest.raises(TypeError):
LIGOTimeGPS(None)

@pytest.mark.parametrize('input_, errstr', [
('test', 'invalid literal for LIGOTimeGPS: test'),
(None, 'cannot convert None (NoneType) to LIGOTimeGPS'),
])
def test_creation_errors(input_, errstr):
with pytest.raises(TypeError) as err:
LIGOTimeGPS(input_)
assert str(err.value) == errstr


def test_str():
Expand Down

0 comments on commit 30fb86a

Please sign in to comment.