Skip to content

docs(cosmo): add examples to traits - #18875

Open
nstarman wants to merge 11 commits into
astropy:mainfrom
nstarman:cosmo/traits/docs
Open

nstarman wants to merge 11 commits into
astropy:mainfrom
nstarman:cosmo/traits/docs

Conversation

@nstarman

@nstarman nstarman commented Nov 11, 2025 •

Copy link
Copy Markdown
Member

Illustrative examples.
The target audience of these classes are devs, whom already have their intended physics and want illustrative examples when writing the code.

  • 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.

@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?

@nstarman nstarman moved this from Triage to In Progress in Cosmology, the Expansion Nov 11, 2025
@nstarman
nstarman marked this pull request as ready for review November 14, 2025 20:56
@nstarman
nstarman requested a review from a team as a code owner November 14, 2025 20:56
@nstarman
nstarman requested a review from adrn November 14, 2025 20:56
@nstarman

Copy link
Copy Markdown
Member Author

CC @preshanth

@preshanth

Copy link
Copy Markdown
Contributor

This is looking good in my opinion. The examples are definitely avoiding defining the functions but instead setting values and I suppose that is intended. Are we going to make clear cut example in a different location ?

@nstarman

Copy link
Copy Markdown
Member Author

Updated to fix a merge conflict.

@nstarman

Copy link
Copy Markdown
Member Author

Are we going to make clear cut example in a different location ?

Expanding on traits.rst would probably be the right place for extended examples.

@nstarman
nstarman requested a review from mhvk November 16, 2025 18:38

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

Hmm, only had a quick look, but my tendency would be not to include examples that are not physically meaningful; I basically do not see how a user is going to be helped by it. See, e.g., my immediate question inline...

Instead, I would suggest to add a more tutorial-like page to the docs where you construct a meaningful cosmologies from these traits. One could start with a De Sitter one and then add realism or so.

Of course, with such a page available you could refer to both LambdaCDM and that page in the docstrings.

A faster solution would be only to point to LambdaCDM in the docstring (haven't checked how readable that is).


>>> @dataclasses.dataclass(frozen=True)
... class ExampleHasBaryons(BaryonComponent):
... Ob0: float

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.

It seems annoying that you would have to define Ob0 when the trait already does so. Doesn't the order of traits imply an order here?

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 not strictly necessary, just often good practice when making a dataclass to declare all fields for exactly the reason you mention: the order of traits implies the order of the fields. Being very explicit can be useful in that regard.

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.

This is an aspect of the abstract-final python pattern.

@nstarman nstarman left a comment

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.

Instead, I would suggest to add a more tutorial-like page to the docs where you construct a meaningful cosmologies from these traits. One could start with a De Sitter one and then add realism or so.

See #18876. With all these trait PRs making pedagogical cosmology classes is much simpler, but still not trivial. A de Sitter matter-only would be simpler than what is in #18876, but still verbose.

Hmm, only had a quick look, but my tendency would be not to include examples that are not physically meaningful; I basically do not see how a user is going to be helped by it. See, e.g., my immediate question inline...

My thought is to provide in the docstirng an immediate example of the API in use.
When constructing novel cosmologies the details of a de Sitter example are probably not going to be that relevant.
Per the use case of the traits, the only people that will see these docstrings are people that are constructing their own cosmology classes. For them I think the following things are very useful:

  1. tutorial docs. We do need to make these!
  2. object.__dir__() to see the list of public methods
  3. object?(?) to see the docstring (& full code).

Right now when you are introspecting in an IDE you get no useful information about the API showing: a trivial construction, what methods are abstract and need overrides, and what is public API. These examples give all 3 useful classes of information.

Of course, with such a page available you could refer to both LambdaCDM and that page in the docstrings.
A faster solution would be only to point to LambdaCDM in the docstring (haven't checked how readable that is).

That uses FLRW so the person would need to work their way up the class hierarchy. Also FLRW is very large and complicated (for performance reasons) and it brings together all the traits. For the trait docstrings I want atomic examples.

I do think pointing to #18876 in the docstrings will be very useful!


>>> @dataclasses.dataclass(frozen=True)
... class ExampleHasBaryons(BaryonComponent):
... Ob0: float

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 not strictly necessary, just often good practice when making a dataclass to declare all fields for exactly the reason you mention: the order of traits implies the order of the fields. Being very explicit can be useful in that regard.

@nstarman

Copy link
Copy Markdown
Member Author

Are we going to make clear cut example in a different location ?

@preshanth Yeah, that would be great to do in the docs. It's a little challenging because the traits are such surgical slices of a cosmology that this would probably have to be a narrative doc walking through building a full cosmology class.

@nstarman

Copy link
Copy Markdown
Member Author

@mhvk, does it make sense that, given the audience that will be reading these docstrings, it's more important to communicate the API than to show a mathematically interesting cosmology.

@nstarman
nstarman requested a review from mhvk December 5, 2025 20:20
@github-actions github-actions Bot added the Close? Tell stale bot that this issue/PR is stale label Apr 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi humans 👋 - this pull request hasn't had any new commits for approximately 4 months. I plan to close this in 30 days if the pull request doesn't have any new commits by then.

In lieu of a stalled pull request, please consider closing this and open an issue instead if a reminder is needed to revisit in the future. Maintainers may also choose to add keep-open label to keep this PR open but it is discouraged unless absolutely necessary.

If this PR still needs to be reviewed, as an author, you can rebase it to reset the clock.

If you believe I commented on this pull request incorrectly, please report this here.

@astrofrog astrofrog modified the milestones: v8.0.0, v8.1.0 May 5, 2026
@github-actions github-actions Bot added the closed-by-bot Closed by stale bot label May 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

I'm going to close this pull request as per my previous message. If you think what is being added/fixed here is still important, please remember to open an issue to keep track of it. Thanks!

If this is the first time I am commenting on this issue, or if you believe I closed this issue incorrectly, please report this here.

@github-actions github-actions Bot closed this May 18, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Cosmology, the Expansion May 18, 2026
@nstarman nstarman reopened this Jul 6, 2026
@github-project-automation github-project-automation Bot moved this from Done to In Progress in Cosmology, the Expansion Jul 6, 2026
@nstarman nstarman added keep-open and removed Close? Tell stale bot that this issue/PR is stale closed-by-bot Closed by stale bot labels Jul 6, 2026
@nstarman
nstarman requested a review from neutrinoceros July 6, 2026 19:54
@nstarman

nstarman commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

@neutrinoceros as far as I can tell, the RTD failure is unrelated.

@nstarman

nstarman commented Sep 13, 2026 •

Copy link
Copy Markdown
Member Author

@neutrinoceros, rebasing to pick up the new tests.

@pllim pllim removed the keep-open label Oct 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

5 participants