Skip to content

Change Table and Column meta to dict and enforce YAML output as ordered map - #16250

Merged
taldcroft merged 10 commits into
astropy:mainfrom
taldcroft:ascii-remove-ordereddict
Apr 16, 2024
Merged

taldcroft merged 10 commits into
astropy:mainfrom
taldcroft:ascii-remove-ordereddict

Conversation

@taldcroft

@taldcroft taldcroft commented Mar 29, 2024 •

Copy link
Copy Markdown
Member

Description

This builds on top of #16240 (which built from #15496) to work toward removing OrderedDict from astropy where possible. This PR does so in a number of places.

Table

  • Table.meta now defaults to dict.
  • Column.meta now defaults to dict.

It is important to remember that these attributes can still be set to any mapping type (e.g. t.meta = OrderedDict(...)), it is just the default type that is changed.

YAML / ECSV

The machinery which helps to serialize Table and Column to YAML (and back) has been updated to always write the Table and Column meta attribute as an ordered map (!!omap in YAML). This change addresses a previous inconsistency: if the user initialized a Table like t = Table(data, meta={'a': 1}) then the YAML would serialize meta as an unordered mapping. This was not the intent. The same held for Column.meta. Now the behavior is consistent and this is reflected in changes to unit tests and docstrings. However, the order of items in the dict is not changed.

This mostly just impacts ECSV. There should not be any issues with back-compatibility. The main change is just following the Table change, namely that reading an ECSV file will always return a Table that has dict meta instead of OrderedDict meta (except of course that past behavior was inconsistent).

Other io.ascii reader classes

These are somewhat unrelated to the more impacting changes but are useful nonetheless.

This PR should be squashed.

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

cc: @MridulS @nstarman

@taldcroft taldcroft added the API change PRs and issues that change an existing API, possibly requiring a deprecation period label Mar 29, 2024
@taldcroft taldcroft added this to the v7.0.0 milestone Mar 29, 2024
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@github-actions

Copy link
Copy Markdown
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosmology changes LGTM. This is reversing the effects of #15484 which had updated meta to be a dict in the table, resulting in the change. This PR effects Table and thus Cosmology.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. So this is making a little churn but should be OK.

@MridulS MridulS mentioned this pull request Mar 31, 2024
1 task

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach! It puts the use of OrderedDict exactly where the problem lies, with yaml, and makes everything else easier. It is especially nice that order is now guaranteed to round-trip.

I assume it is on purpose that you do not (yet) change the default for meta itself?

@taldcroft

Copy link
Copy Markdown
Member Author

I assume it is on purpose that you do not (yet) change the default for meta itself?

Yes, but that's a great question. I was thinking about making that change as a separate PR. And as usual when you start poking at some code you see little problems. E.g. for some reason Quantity info is not enforcing that meta should be dict-like:

In [2]: q = [1, 2] * u.m

In [3]: q.info.meta = "wrong"

In [4]: q.info.meta
Out[4]: 'wrong'

So anyway, maybe a follow-up PR that changes the default and fixes things like that?

@mhvk

mhvk commented Apr 4, 2024

Copy link
Copy Markdown
Contributor

OK, let's just deal with getting YAML right here!

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a look through everything again and basically this looks all great to me. Probably good to have a changelog entry!

@taldcroft
taldcroft force-pushed the ascii-remove-ordereddict branch from 0f4753e to 626844a Compare April 4, 2024 14:12
@taldcroft
taldcroft marked this pull request as ready for review April 4, 2024 14:12
@taldcroft
taldcroft requested review from a team and dhomeier as code owners April 4, 2024 14:12
@taldcroft
taldcroft requested a review from nstarman April 4, 2024 14:12
@taldcroft
taldcroft requested a review from mhvk April 4, 2024 14:13
@taldcroft

taldcroft commented Apr 4, 2024 •

Copy link
Copy Markdown
Member Author

@mhvk - I've added changelog entries and a What's New. The expectation is that the What's New will be expanded to include the next PR to change the default to dict in MetaData itself.

AFAIK this PR is complete and ready for review. (Of course CI often disagrees...)

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Main comments are to update a comment and docstring.

Also, the readthedocs failures may be related to 6.1 having been branched...:

/home/docs/checkouts/readthedocs.org/user_builds/astropy/checkouts/16250/docs/
index_getting_started.rst:7: WARNING: toctree contains reference to document 'whatsnew/6.1' that doesn't have a title: no link will be generated

Comment thread astropy/table/column.py Outdated
class BaseColumn(_ColumnGetitemShim, np.ndarray):
meta = MetaData(default_factory=OrderedDict)
meta = MetaData(default_factory=dict)
# It's important that the metadata is an OrderedDict so that the order of

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you adjust this comment? Or perhaps just remove it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment thread astropy/table/meta.py
OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
"""
omap = OrderedDict()
omap = {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the docstring needs to be adjusted here! Note that the doctest is skipped - I think that may be from a time when yaml was not a required dependency.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up removing the example entirely. It turns out that it didn't work as shown. First there were no imports so there was ambiguity about the yaml symbol (from astropy or pyyaml?). But more importantly it didn't show that this private function needs to be registered as a constructor in a custom Loader subclass. The comment now just refers to the astropy function where it is used and that should be fine.

@taldcroft
taldcroft force-pushed the ascii-remove-ordereddict branch from 626844a to ec0d4e1 Compare April 4, 2024 17:48
@taldcroft

Copy link
Copy Markdown
Member Author

@mhvk - thanks for the quick review, your comments have been addressed. I just rebased again and with any luck the RTD will pass now.

@taldcroft

Copy link
Copy Markdown
Member Author

And as a reminder I prefer that this be squashed.

Comment thread astropy/table/meta.py
License: Unspecified

This is the same as SafeConstructor.construct_yaml_omap(),
except the data type is changed to OrderedDict() and setitem is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still true?

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll approve now, so you can merge when tests pass, but see my single comment.

@taldcroft

Copy link
Copy Markdown
Member Author

@pllim @saimn @astrofrog - I'm still seeing an RTD warning /home/docs/checkouts/readthedocs.org/user_builds/astropy/checkouts/16250/docs/index_getting_started.rst:7: WARNING: toctree contains reference to document 'whatsnew/6.1' that doesn't have a title: no link will be generated which is causing the RTD build to fail.

I rebased on main on Friday. Any thoughts on fixing this?

@saimn

saimn commented Apr 9, 2024

Copy link
Copy Markdown
Contributor

Should be fixed with #16272.

@nstarman nstarman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosmology looks G2G. Thanks!

@taldcroft
taldcroft force-pushed the ascii-remove-ordereddict branch from ec0d4e1 to 81b3c81 Compare April 16, 2024 20:47
@taldcroft
taldcroft enabled auto-merge (squash) April 16, 2024 20:48
@taldcroft

Copy link
Copy Markdown
Member Author

I rebased on main which will hopefully fix the RTD failure.

@taldcroft
taldcroft merged commit d9ef612 into astropy:main Apr 16, 2024
@taldcroft
taldcroft deleted the ascii-remove-ordereddict branch April 16, 2024 21:06
d-giles referenced this pull request in d-giles/astropy-abdufork Jul 26, 2024
…ed map (#16250)

* Use OrderedDict/omap sandwich in YAML

* Removed OrderedDict from io.ascii

This pull request removes OrderedDict from files in io.ascii.

The files are: core.py , daophot.py , ecsv.py , fastbasic.py , ipac.py and test_read.py.
The OrderedDicts are normal dictionaries now.

* fixed indent error

* Remove unused imports and ruff formatting

* Use dict as default factory for meta instead of OrderedDict

* Table and column meta default to dict, in YAML always !!omap

* Make testing (esp ECSV) more robust for meta

* Add change log entries

* Add What's New entry

* Minor doc updates

---------

Co-authored-by: alexandrecesarba <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API change PRs and issues that change an existing API, possibly requiring a deprecation period cosmology io.ascii table utils whatsnew-needed

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants