Skip to content

Cosmo as a dataclass - #15484

Merged
eerovaher merged 14 commits into
astropy:mainfrom
nstarman:cosmo-cosmo-dataclass
Mar 29, 2024
Merged

eerovaher merged 14 commits into
astropy:mainfrom
nstarman:cosmo-cosmo-dataclass

Conversation

@nstarman

@nstarman nstarman commented Oct 15, 2023 •

Copy link
Copy Markdown
Member

This PR changes Cosmology and its subclasses to dataclasses.dataclass objects for versions py3.10+.

Why change?

Well, this has long been the intended plan. Cosmology is structured very similarly to the dataclass paradigm (see cosmology.Parameter) but small differences meant that they were not compatible.

Dataclasses bring a number of advantages over normal classes, particularly for classes that are defined by their attributes (so not dict-like things).

  1. straightforward declaration of the fields / attributes
  2. no need to define most dunder methods.
  3. built in static typing compatibility
  4. support for match statements
  5. easy to make immutable structures
  6. introspection tools like fields and asdict, etc.
  7. easier to dynamically define subclasses
  8. deep support in transpilation / compilation libraries, e.g. mypyc.
  9. ...
  • 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.

@nstarman nstarman added Enhancement API change PRs and issues that change an existing API, possibly requiring a deprecation period labels Oct 15, 2023
@nstarman nstarman added this to the v6.0 milestone Oct 15, 2023
@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?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • 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?

@nstarman
nstarman force-pushed the cosmo-cosmo-dataclass branch 9 times, most recently from 6be68d6 to ee557a6 Compare October 16, 2023 13:37
@nstarman
nstarman force-pushed the cosmo-cosmo-dataclass branch 13 times, most recently from 5e03f6a to 6728751 Compare October 24, 2023 00:31
@nstarman

nstarman commented Jan 11, 2024 •

Copy link
Copy Markdown
Member Author

Furthermore, and out of curiosity, can I ask for a short explanation of what makes Python 3.10's data classes better for existing code ?

dataclasses.dataclass objects have a number of distinct advantages

  1. Less code => reduced code debt
  2. Natural definition of immutability
  3. Better introspection (annotations and tools from dataclasses)
  4. Better I/O (tools from dataclasses)
  5. Enforced good code structure.
  6. ensure compatibility with mypyc transpilation.

Except for the time required to upgrade the code I submit the opposite, why would we not make a class a dataclass?

@neutrinoceros

Copy link
Copy Markdown
Contributor

why would we not make a class a dataclass?

A concern I heard about data classes is startup time overhead.
There are also alternatives like attrs, which is historically a precursor to data classes, but it is still actively developed as a third party package.
But I don't think we need to worry about the startup overhead too much for a library, and I just mentioned attrs for good measure, but if we can live happily with the standard lib implementation, no need to reach out for another third party dependency !

@nstarman

Copy link
Copy Markdown
Member Author

@eerovaher @WilliamJamieson @neutrinoceros, now that #15603 is in, this PR is ready as well! It will be great to finally upgrade the architecture of Cosmology. This should make a lot of other features easier to implement.

@neutrinoceros neutrinoceros 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 set aside some time to finish this review tomorrow, in the mean time here are a couple (very) small questions.

Any
Results of evaluating the Cosmology method.
"""
# TODO: speed up using ``replace``

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.

Should this be done in this PR ? Otherwise, why not ?

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.

It's just out of scope. This function has to comply with requirements for making Astropy Models.

Comment thread astropy/cosmology/core.py Outdated

@neutrinoceros neutrinoceros 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.

This time I was able to do a complete read through. This looks outstanding !

A broad (but very minor) comment is that I'm not sure about the name dataclass_decorator. How about maybe something more descriptive of its application domain like cosmology_dataclass ?

Comment thread astropy/cosmology/_io/tests/test_html.py
Comment thread astropy/cosmology/core.py
Comment thread astropy/cosmology/core.py Outdated
Comment thread astropy/cosmology/flrw/tests/test_base.py
Comment thread astropy/cosmology/flrw/tests/test_base.py
Comment thread astropy/cosmology/parameter/tests/test_parameter.py
Comment thread astropy/cosmology/tests/test_core.py Outdated
Comment thread astropy/cosmology/tests/test_core.py
Comment thread astropy/utils/compat/misc.py Outdated
Comment thread docs/changes/cosmology/15484.api.rst Outdated
Comment thread astropy/cosmology/core.py
# Derived quantities:
# Dark matter density; matter - baryons, if latter is not None.
self._Odm0 = None if Ob0 is None else (self._Om0 - self._Ob0)
object.__setattr__(

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 want to collect all these object.__setattr__ ancillary variables into a more efficient storage object. But I think that should be done in a future PR.

Comment thread astropy/cosmology/core.py
Comment thread astropy/cosmology/flrw/tests/test_base.py
Comment thread astropy/cosmology/flrw/tests/test_base.py
Comment thread docs/whatsnew/6.1.rst Outdated

@neutrinoceros neutrinoceros 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.

With your latest batch of replies, I'm happy to sign this off !

@nstarman

Copy link
Copy Markdown
Member Author

@eerovaher @WilliamJamieson, the feature freeze is coming up. What do y'all think of this PR?

@eerovaher

Copy link
Copy Markdown
Member

I agree we should try to get this merged before the feature freeze. I'll make time to have a look at it tomorrow. Luckily @neutrinoceros has already reviewed it, so I don't expect that any big changes are going to be needed.

@eerovaher eerovaher 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.

I do have a few comments, but it should be possible to address them before the feature freeze.

Do you plan editing the commit history yourself? Currently there are two different commits that both Make Cosmology a dataclass, so a normal merge doesn't seem to be the right thing to do, but you don't want this to be automatically squashed either.

Comment thread astropy/cosmology/_utils.py
Comment thread astropy/cosmology/core.py
Comment thread astropy/cosmology/core.py Outdated
Comment thread astropy/cosmology/core.py Outdated
Comment thread astropy/cosmology/core.py
all_vars["name"].__set__(self, name)
all_vars["meta"].__set__(self, OrderedDict(meta or {}))

def __post_init__(self): # noqa: B027

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.

The current docstring is very strongly suggesting that this should be an abstract method (which would also get rid of the noqa), so the docstring should be rewritten.

)
from astropy.cosmology.parameter._core import MISSING

##############################################################################

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.

I think he meant line 19.

Comment thread docs/changes/cosmology/15484.api.rst Outdated
Comment thread docs/whatsnew/6.1.rst Outdated
Comment thread docs/whatsnew/6.1.rst Outdated
now a :func:`~dataclasses.dataclass`. This means that the :mod:`dataclasses` machinery
can be used to work with :class:`~astropy.cosmology.Cosmology` objects. For example::

.. doctest-skip::

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.

Why are the doctests being skipped?

Comment thread docs/whatsnew/6.1.rst Outdated
@nstarman

Copy link
Copy Markdown
Member Author

Do you plan editing the commit history yourself? Currently there are two different commits that both Make Cosmology a dataclass, so a normal merge doesn't seem to be the right thing to do, but you don't want this to be automatically squashed either.

Yes, this is a product of an interactive rebase. I'll fix it up.

nstarman and others added 14 commits March 28, 2024 20:52
Signed-off-by: nstarman <[email protected]>

Signed-off-by: nstarman <[email protected]>
py3.13+ will have the `converter` arg, eliminating this class.

Co-authored-by: Clément Robert <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>

Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>

Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
@nstarman

nstarman commented Mar 29, 2024 •

Copy link
Copy Markdown
Member Author

🎉 thanks all for the reviews!!

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 Docs Enhancement testing whatsnew-needed

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants