Repository: astropy/astropy

Issue:
FITS I/O : Comment dropped when writing a keyword and comment header entry in BinTableHDU extension
I am trying to add a header keyword with a float value and a comment. I want to add it to the BinTable extension of my fits file. However, for some reason fits is not writing the comment and just writing the value.

Hence to investigate this I wrote a small test code where I create a similar header and add it to a test fits file. Here I add it to both, the PrimaryHDU and the BinTableHDU extension. The problem is that fits writes the comment in case of the PrimaryHDU header but not in the BinTableHDU extension header. I am attaching the test code.  Look at the header keyword 'RA' in both the outputs.

```datahdr = fits.Header()
datahdr['RA'] = (158.51354, "Pointing RA in deg")     # Add a keyword with comment

aHDU = fits.HDUList()
aHDU.append(fits.PrimaryHDU(header=datahdr))
aHDU.append(fits.BinTableHDU(data=Table([[12], [123]]), header=datahdr, name='test'))
aHDU.writeto("test.fits", overwrite=True)

hdr = fits.getheader("test.fits", 1)
hdr

XTENSION= 'BINTABLE'           / binary table extension
BITPIX  =                    8 / array data type
NAXIS   =                    2 / number of array dimensions
NAXIS1  =                   16 / length of dimension 1
NAXIS2  =                    1 / length of dimension 2
PCOUNT  =                    0 / number of group parameters
GCOUNT  =                    1 / number of groups
TFIELDS =                    2 / number of table fields
RA      =            158.51354
EXTNAME = 'TEST    '           / extension name
TTYPE1  = 'col0    '
TFORM1  = 'K       '
TTYPE2  = 'col1    '
TFORM2  = 'K       '

hdr = fits.getheader("test.fits", 0)
hdr

SIMPLE  =                    T / conforms to FITS standard
BITPIX  =                    8 / array data type
NAXIS   =                    0 / number of array dimensions
EXTEND  =                    T
RA      =            158.51354 / Pointing RA in deg```

Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 84bc59d0bd..536a10160a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -2248,6 +2248,9 @@ astropy.io.ascii
 astropy.io.fits
 ^^^^^^^^^^^^^^^
 
+- Fix ``Header.update`` which was dropping the comments when passed
+  a ``Header`` object. [#8840]
+
 astropy.io.misc
 ^^^^^^^^^^^^^^^
 
diff --git a/astropy/io/fits/header.py b/astropy/io/fits/header.py
index 29d7a4f5d3..0518aa5c0f 100644
--- a/astropy/io/fits/header.py
+++ b/astropy/io/fits/header.py
@@ -1124,6 +1124,9 @@ class Header:
 
         if other is None:
             pass
+        elif isinstance(other, Header):
+            for card in other.cards:
+                self._update(card)
         elif hasattr(other, 'items'):
             for k, v in other.items():
                 update_from_dict(k, v)
diff --git a/astropy_helpers b/astropy_helpers
index ba3734222a..20deaeb79b 160000
--- a/astropy_helpers
+++ b/astropy_helpers
@@ -1,1 +1,1 @@
-Subproject commit ba3734222a40f4c2864c375c6639f32cd9df06cd
+Subproject commit 20deaeb79b0518fa9bb9dbcd46f1baed79b6f736


Test patch:
diff --git a/astropy/io/fits/tests/test_header.py b/astropy/io/fits/tests/test_header.py
index e50ded6a66..84f75651a3 100644
--- a/astropy/io/fits/tests/test_header.py
+++ b/astropy/io/fits/tests/test_header.py
@@ -220,11 +220,19 @@ class TestHeaderFunctions(FitsTestCase):
         header = fits.Header()
         header.update({'FOO': ('BAR', 'BAZ')})
         header.update(FakeHeader([('A', 1), ('B', 2, 'comment')]))
+
         assert set(header.keys()) == {'FOO', 'A', 'B'}
         assert header.comments['B'] == 'comment'
 
+        # test that comments are preserved
+        tmphdr = fits.Header()
+        tmphdr['HELLO'] = (1, 'this is a comment')
+        header.update(tmphdr)
+        assert set(header.keys()) == {'FOO', 'A', 'B', 'HELLO'}
+        assert header.comments['HELLO'] == 'this is a comment'
+
         header.update(NAXIS1=100, NAXIS2=100)
-        assert set(header.keys()) == {'FOO', 'A', 'B', 'NAXIS1', 'NAXIS2'}
+        assert set(header.keys()) == {'FOO', 'A', 'B', 'HELLO', 'NAXIS1', 'NAXIS2'}
         assert set(header.values()) == {'BAR', 1, 2, 100, 100}
 
     def test_update_comment(self):