Repository: astropy/astropy

Issue:
Passing a Time instance to a subclass of Time errors
We recently implemented a subclass of Time in SunPy (for backwards compatibility reasons) and we have encounted an issue when passing an instance of `astropy.time.Time` to our subclass:

```python
>>> from astropy.time import Time
>>> class _Time(Time):
...     pass
...
>>> t = Time.now()
>>> _Time(t)
Traceback (most recent call last):
  File <stdin>, line 1, in <module>
  File /home/vn-ki/.conda/envs/sunpy-dev/lib/python3.6/site-packages/astropy/time/core.py, line 441, in __repr__
    .format(self.__class__.__name__, self.scale, self.format,
  File /home/vn-ki/.conda/envs/sunpy-dev/lib/python3.6/site-packages/astropy/time/core.py, line 1147, in __getattr__
    return self.__getattribute__(attr)
  File /home/vn-ki/.conda/envs/sunpy-dev/lib/python3.6/site-packages/astropy/time/core.py, line 450, in scale
    return self._time.scale
  File /home/vn-ki/.conda/envs/sunpy-dev/lib/python3.6/site-packages/astropy/time/core.py, line 1147, in __getattr__
    return self.__getattribute__(attr)
AttributeError: '_Time' object has no attribute '_time'
```

Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 474bd7ed15..13f272307f 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -984,6 +984,9 @@ astropy.tests
 astropy.time
 ^^^^^^^^^^^^
 
+- Fixed a bug in Time that raised an error when initializing a subclass of Time
+  with a Time object. [#7453]
+
 astropy.units
 ^^^^^^^^^^^^^
 
diff --git a/astropy/time/core.py b/astropy/time/core.py
index 10cc4ab3ce..a5b91ca168 100644
--- a/astropy/time/core.py
+++ b/astropy/time/core.py
@@ -328,7 +328,7 @@ class Time(ShapedLikeNDArray):
         else:
             self.location = None
 
-        if isinstance(val, Time):
+        if isinstance(val, self.__class__):
             # Update _time formatting parameters if explicitly specified
             if precision is not None:
                 self._time.precision = precision


Test patch:
diff --git a/astropy/time/tests/test_basic.py b/astropy/time/tests/test_basic.py
index a6879de4af..c8ad2acee3 100644
--- a/astropy/time/tests/test_basic.py
+++ b/astropy/time/tests/test_basic.py
@@ -329,7 +329,7 @@ class TestBasic():
             # test for conversion to local scale
             scale3 = 'local'
             with pytest.raises(ScaleValueError):
-                    t2 = getattr(t1, scale3)
+                t2 = getattr(t1, scale3)
 
     def test_creating_all_formats(self):
         """Create a time object using each defined format"""
@@ -1352,3 +1352,17 @@ def test_setitem_deltas():
     t[1] = 3
     assert not hasattr(t, '_delta_tdb_tt')
     assert not hasattr(t, '_delta_ut1_utc')
+
+
+def test_subclass():
+    """Check that we can initialize subclasses with a Time instance."""
+    # Ref: Issue gh-#7449 and PR gh-#7453.
+
+    class _Time(Time):
+        pass
+
+    t1 = Time('1999-01-01T01:01:01')
+    t2 = _Time(t1)
+
+    assert t2.__class__ == _Time
+    assert t1 == t2