Skip to content

Commit

Permalink
Merge pull request #23 from sot/init-none
Browse files Browse the repository at this point in the history
Fix incompatibility with DateTime init with None
  • Loading branch information
taldcroft authored Nov 9, 2020
2 parents fbcc404 + 48bc3f1 commit 27b0abe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cxotime/cxotime.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,21 @@ class CxoTime(Time):
def __new__(cls, *args, **kwargs):
# Handle the case of `CxoTime()` which returns the current time. This is
# for compatibility with DateTime.
if not args:
if not args or (len(args) == 1 and args[0] is None):
if not kwargs:
# Stub in a value for `val` so super()__new__ can run since `val`
# is a required positional arg.
args = (None, )
else:
raise ValueError('cannot supply keyword arguments with no time value')
return super().__new__(cls, *args, **kwargs)

def __init__(self, *args, **kwargs):
if len(args) == 1 and args[0] is None:
# Compatibility with DateTime and allows kwarg default of None with
# input casting like `date = CxoTime(date)`.
args = ()

if args:
if args[0].__class__.__name__ == 'DateTime':
if len(args) > 1:
Expand Down
10 changes: 10 additions & 0 deletions cxotime/tests/test_cxotime.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def test_cxotime_now(now_method):
CxoTime(scale='utc')


def test_cxotime_now_by_none():
ct_now = CxoTime(None)
t_now = Time.now()
assert abs((ct_now - t_now).to_value(u.s)) < 0.1

with pytest.raises(ValueError,
match='cannot supply keyword arguments with no time value'):
CxoTime(None, scale='utc')


def test_cxotime_from_datetime():
secs = DateTime(np.array(['2000:001', '2015:181:23:59:60.500', '2015:180:01:02:03.456'])).secs
dts = DateTime(secs)
Expand Down

0 comments on commit 27b0abe

Please sign in to comment.