diff options
author | Vincent Driessen <vincent@3rdcloud.com> | 2012-10-27 04:12:10 -0700 |
---|---|---|
committer | Vincent Driessen <vincent@3rdcloud.com> | 2012-10-27 04:12:10 -0700 |
commit | 11a6f98075757621276cd46c3afda6f917f44269 (patch) | |
tree | ff856e408dd75afb803ac9239d3c4a6c636f220d | |
parent | add60fd530e1d5d324fa4991d99ff915b72e36e6 (diff) | |
parent | dd5b7b8785dcc11b74b0c40e763e9780834a36d2 (diff) | |
download | times-11a6f98075757621276cd46c3afda6f917f44269.tar.gz |
Merge pull request #8 from jparise/typeerrors
Raise TypeErrors for invalid argument types.
-rw-r--r-- | tests/test_times.py | 6 | ||||
-rw-r--r-- | times/__init__.py | 10 |
2 files changed, 6 insertions, 10 deletions
diff --git a/tests/test_times.py b/tests/test_times.py index 3fedef9..b887c3d 100644 --- a/tests/test_times.py +++ b/tests/test_times.py @@ -102,7 +102,7 @@ class TestTimes(TestCase): def test_to_universal_rejects_non_date_arguments(self): """to_universal rejects non-date arguments""" - with self.assertRaises(ValueError): + with self.assertRaises(TypeError): times.to_universal([1, 2, 3]) @@ -127,7 +127,7 @@ class TestTimes(TestCase): def test_convert_non_numeric_from_unix(self): """from_unix refuses to accept non-numeric input""" - with self.assertRaises(ValueError): + with self.assertRaises(TypeError): times.from_unix('lol') @@ -141,7 +141,7 @@ class TestTimes(TestCase): def test_convert_non_numeric_to_unix(self): """to_unix refuses to accept non-numeric input""" - with self.assertRaises(ValueError): + with self.assertRaises(TypeError): times.to_unix('lol') diff --git a/times/__init__.py b/times/__init__.py index e446f19..345ce9c 100644 --- a/times/__init__.py +++ b/times/__init__.py @@ -36,7 +36,7 @@ def to_universal(local_dt, timezone=None): def from_local(local_dt, timezone=None): """Converts the given local datetime to a universal datetime.""" if not isinstance(local_dt, datetime.datetime): - raise ValueError('First argument should be int, float or datetime.') + raise TypeError('Expected a datetime object') if timezone is not None: if local_dt.tzinfo is not None: @@ -63,9 +63,7 @@ def from_unix(ut): time. Assumes the input is in UTC, as `time.time()` does. """ if not isinstance(ut, (int, float)): - raise ValueError( - 'First argument to from_unix should be an int or float' - ) + raise TypeError('Expected an int or float value') return datetime.datetime.utcfromtimestamp(float(ut)) @@ -85,9 +83,7 @@ def to_local(dt, timezone): def to_unix(dt): """Converts a datetime object to unixtime""" if not isinstance(dt, datetime.datetime): - raise ValueError( - 'First argument to to_unix should be a datetime object' - ) + raise TypeError('Expected a datetime object') return calendar.timegm(dt.utctimetuple()) |