Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Raise TypeErrors for invalid argument types. #8

Merged
merged 2 commits into from

2 participants

Jon Parise Vincent Driessen
Jon Parise

No description provided.

Vincent Driessen nvie merged commit 11a6f98 into from
Vincent Driessen
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 10 deletions.
  1. +3 −3 tests/test_times.py
  2. +3 −7 times/__init__.py
6 tests/test_times.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')
10 times/__init__.py
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())
Something went wrong with that request. Please try again.