Skip to content

Commit

Permalink
Avoid duplicate storage of info in serialized columns.
Browse files Browse the repository at this point in the history
As is, info is store on any primary data column, but also in
the serialized column, which makes the header unnecessarily long.
  • Loading branch information
mhvk committed Dec 18, 2021
1 parent 698d2e8 commit 754aac9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion astropy/table/serialize.py
Expand Up @@ -134,7 +134,8 @@ def _represent_mixin_as_column(col, name, new_cols, mixin_cols,
# MaskedColumn). For primary data, we attempt to store any info on
# the format, etc., on the column, but not for ancillary data (e.g.,
# no sense to use a float format for a mask).
if data_attr == col.info._represent_as_dict_primary_data:
is_primary = data_attr == col.info._represent_as_dict_primary_data
if is_primary:
new_name = name
new_info = info
else:
Expand All @@ -146,6 +147,10 @@ def _represent_mixin_as_column(col, name, new_cols, mixin_cols,
and np.any(data.mask)) else Column
new_cols.append(col_cls(data, name=new_name, **new_info))
obj_attrs[data_attr] = SerializedColumn({'name': new_name})
if is_primary:
# Don't store info in the __serialized_columns__ dict for this column
# since this is redundant with info stored on the new column.
info = {}
else:
# recurse. This will define obj_attrs[new_name].
_represent_mixin_as_column(data, new_name, new_cols, obj_attrs)
Expand Down
14 changes: 14 additions & 0 deletions astropy/table/tests/test_mixin.py
Expand Up @@ -817,6 +817,20 @@ def test_represent_mixins_as_columns_unit_fix():
serialize.represent_mixins_as_columns(t)


def test_primary_data_column_gets_description():
"""
If the mixin defines a primary data column, that should get the
description, format, etc., so no __info__ should be needed.
"""
t = QTable({'a': [1, 2] * u.m})
t['a'].info.description = 'parrot'
t['a'].info.format = '7.2f'
tser = serialize.represent_mixins_as_columns(t)
assert '__info__' not in tser.meta['__serialized_columns__']['a']
assert tser['a'].format == '7.2f'
assert tser['a'].description == 'parrot'


def test_skycoord_with_velocity():
# Regression test for gh-6447
sc = SkyCoord([1], [2], unit='deg', galcen_v_sun=None)
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/table/12607.bugfix.rst
@@ -0,0 +1,2 @@
Avoid duplicate storage of info in serialized columns if the column
used to serialize already can hold that information.

0 comments on commit 754aac9

Please sign in to comment.