Permalink
Browse files

Merge pull request #8 from jparise/typeerrors

Raise TypeErrors for invalid argument types.
  • Loading branch information...
2 parents add60fd + dd5b7b8 commit 11a6f98075757621276cd46c3afda6f917f44269 @nvie committed Oct 27, 2012
Showing with 6 additions and 10 deletions.
  1. +3 −3 tests/test_times.py
  2. +3 −7 times/__init__.py
View
@@ -102,7 +102,7 @@ def test_to_universal_rejects_no_tzinfo(self): # noqa
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 @@ def test_convert_unix_time_to_datetime(self): # noqa
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 @@ def test_convert_datetime_to_unix_time(self): # noqa
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')
View
@@ -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())

0 comments on commit 11a6f98

Please sign in to comment.