Skip to content

typing cosmology's io#15917

Merged
eerovaher merged 2 commits into
astropy:mainfrom
nstarman:cosmo-typing-io
Oct 22, 2024
Merged

typing cosmology's io#15917
eerovaher merged 2 commits into
astropy:mainfrom
nstarman:cosmo-typing-io

Conversation

@nstarman
Copy link
Copy Markdown
Member

@nstarman nstarman commented Jan 20, 2024

Requires #15916 (currently based on that PR. will need a rebase when it's merged).

I'm looking to minimally type cosmology's I/O module. In doing the types I've noticed a number of places where improving the code will enable us to replace All annotations. Let's do code changes in followup PRs. I modified one docstring to get Mypy to be happy.

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

Comment thread astropy/cosmology/typing.py
@nstarman nstarman mentioned this pull request Jan 20, 2024
1 task
@nstarman nstarman requested review from eerovaher and mhvk January 20, 2024 22:02
@nstarman nstarman marked this pull request as ready for review January 20, 2024 22:33
@nstarman nstarman requested a review from a team as a code owner January 20, 2024 22:33
@mhvk
Copy link
Copy Markdown
Contributor

mhvk commented Jan 20, 2024

Thanks, this looks like a great start. However, I realize even more than with #15916 that I'm not suited to review this: I don't understand all those underscored TypeVar and don't really have time to investigate. So, I'll unsubscribe.

@mhvk mhvk removed their request for review January 20, 2024 22:47
@neutrinoceros
Copy link
Copy Markdown
Contributor

I'm happy to fill in as a reviewer if it helps !

Copy link
Copy Markdown
Contributor

@neutrinoceros neutrinoceros left a comment

Choose a reason for hiding this comment

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

Here's a first round. This is my first time reviewing actual usage of TypeVar with covariance/contravariance (as opposed to reading PEPs for years...), so I've intentionally avoided reviewing astropy/cosmology/_io/mapping.py for now and will come back to it when my first questions are addressed. Thanks !

Comment thread astropy/cosmology/_io/ecsv.py
Comment thread astropy/cosmology/_io/html.py Outdated
Comment thread astropy/cosmology/_io/latex.py
Comment thread astropy/cosmology/_io/yaml.py
Comment thread astropy/cosmology/_io/model.py
Comment thread astropy/cosmology/_io/ecsv.py Outdated
Copy link
Copy Markdown
Member

@eerovaher eerovaher left a comment

Choose a reason for hiding this comment

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

I had a quick look. Some of the questions or suggestions I have are applicable to quite a few locations and I didn't highlight all of them.

Comment thread astropy/cosmology/_io/html.py Outdated

def html_identify(origin, filepath, fileobj, *args, **kwargs):
def html_identify(
origin: Any, filepath: Any, fileobj: Any, *args: Any, **kwargs: Any
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 there so many parameters that are not used?

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 how the universal I/O identify functions are written. I suppose some formats need them.

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.

As I already mentioned elsewhere, if we need these arguments but we don't really care about their type then we should be annotating them as object instead of Any. The way I interpret Any is that not all types are valid, but the valid types are too difficult to write down because of dynamical code or simply not worth the effort, whereas object means that we know for sure that any type is valid.

Comment thread astropy/cosmology/_io/mapping.py Outdated
Comment thread astropy/cosmology/_io/mapping.py Outdated
Comment thread astropy/cosmology/_io/mapping.py Outdated
Comment thread astropy/cosmology/_io/mapping.py Outdated
Comment thread astropy/cosmology/_io/mapping.py Outdated
@nstarman nstarman force-pushed the cosmo-typing-io branch 2 times, most recently from 6b697fd to ebabffa Compare February 8, 2024 15:52
Comment thread astropy/cosmology/_io/cosmology.py Outdated
Comment thread astropy/cosmology/_io/cosmology.py Outdated
Comment thread astropy/cosmology/_io/cosmology.py Outdated
Comment thread astropy/cosmology/_io/ecsv.py Outdated
Comment thread astropy/cosmology/_io/html.py Outdated
Comment thread astropy/cosmology/_io/mapping.py
Comment thread astropy/cosmology/_io/mapping.py
@nstarman nstarman force-pushed the cosmo-typing-io branch 2 times, most recently from 9a9617e to b57931e Compare February 8, 2024 23:38
Copy link
Copy Markdown
Member

@eerovaher eerovaher left a comment

Choose a reason for hiding this comment

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

I have a few remarks and questions. It would also be a good idea to have another look at all the Any annotations and to replace them with object where reasonable.

Comment thread astropy/cosmology/_io/cosmology.py
Comment thread astropy/cosmology/_io/cosmology.py Outdated
cosmo : `~astropy.cosmology.Cosmology`
The cosmology to return.
*args
*args : Any
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.

Do you know what the NumPy docstring convention is? Is this supposed to be Any or object?

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 do not think they have specific guidance on this.

Comment thread astropy/cosmology/_io/ecsv.py Outdated
Comment thread astropy/cosmology/_io/html.py Outdated
Comment thread astropy/cosmology/_io/model.py
Comment thread astropy/cosmology/_io/model.py
Comment thread astropy/cosmology/_io/cosmology.py Outdated

def cosmology_identify(origin, format, *args, **kwargs):
def cosmology_identify(
origin: str, format: str | None, *args: Any, **kwargs: object
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 is the annotation for format so specific? From the code it is clear that format could be any object and the code would still work. There's a couple more similar cases in the patch (mostly involving Literal).

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.

From the code it is clear that format could be any object and the code would still work

While true, None and str are the only types that actually make sense here, from a user perspective (I think ?). The fact that there are no actual type enforcement at runtime doesn't seem relevant to me: it's an implementation detail, which shouldn't justify making annotations less useful.

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.

To be honest I have trouble understanding why format exists. If I want to know if something is identifiable as a Cosmology then simply calling cosmology_identify() should be sufficient and having the option of specifying format="astropy.cosmology" doesn't seem to have any purpose.

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 thought it was a requirement on the signature of functions that are passed to _UnifiedIORegistryBase.register_identifier but it doesn't actually look to be the case... I don't get it either.

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.

The I/O in Cosmology was based on the implementation from table.

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.

Could you point out the specific lines of code?

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.

Sure:

def is_hdf5(origin, filepath, fileobj, *args, **kwargs):
,
def parquet_identify(origin, filepath, fileobj, *args, **kwargs):
,
def is_votable(origin, filepath, fileobj, *args, **kwargs):
are all examples.

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.

But none of these have a format parameter.

Copy link
Copy Markdown
Member Author

@nstarman nstarman Apr 28, 2024

Choose a reason for hiding this comment

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

They are the same as "filepath". But here we are not writing, so it's "format". E.g. "parquet_identify" does a check on filepath to determine it ends in ". parquet" or "parq"

Copy link
Copy Markdown
Member

@eerovaher eerovaher Apr 29, 2024

Choose a reason for hiding this comment

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

For type checking purposes a function argument annotation should be as general as possible. A more specific annotation can be justified if it is simpler but still helpful to users. In this case format: object would be more useful for type checking than format: str | None and the latter can't be justified by its helpfulness because I can't think of any use cases where I'd want to specify format, meaning that the entire argument is useless regardless of how it is annotated.

Copy link
Copy Markdown
Contributor

@neutrinoceros neutrinoceros left a comment

Choose a reason for hiding this comment

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

Sorry I missed your request for review for so long !

Comment thread astropy/cosmology/_io/cosmology.py Outdated
Comment thread astropy/cosmology/_io/ecsv.py Outdated
Comment thread astropy/cosmology/_io/mapping.py Outdated
@nstarman nstarman force-pushed the cosmo-typing-io branch 2 times, most recently from 929c360 to a8844ad Compare February 15, 2024 02:22
@astrofrog astrofrog modified the milestones: v6.1.0, v7.0.0 Apr 4, 2024
@nstarman nstarman marked this pull request as draft April 28, 2024 01:33
@nstarman
Copy link
Copy Markdown
Member Author

Let's do #15852 first

Comment thread astropy/cosmology/_io/cosmology.py
@mhvk mhvk added the typing related to type annotations label May 30, 2024
@github-actions github-actions Bot added the Close? Tell stale bot that this issue/PR is stale label Oct 17, 2024
@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.

@nstarman nstarman marked this pull request as ready for review October 18, 2024 19:38
@nstarman nstarman modified the milestones: v7.0.0, v7.1.0 Oct 18, 2024
nstarman and others added 2 commits October 18, 2024 15:39
Signed-off-by: nstarman <[email protected]>
Co-authored-by: Clément Robert <[email protected]>
Co-authored-by: Eero Vaher <[email protected]>
Signed-off-by: nstarman <[email protected]>
@neutrinoceros
Copy link
Copy Markdown
Contributor

Is this blocked any how ? I'm seeing some unresolved threads from @eerovaher's review but they're both marked as outdated.

Copy link
Copy Markdown
Member

@eerovaher eerovaher left a comment

Choose a reason for hiding this comment

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

I don't understand what many of the functions are doing (e.g. why are there so many parameters that are not used in their functions at all?), but the annotations seem to be consistent both with the docstrings and what the code seems to be doing. Hopefully the annotations will help with sorting out the messy bits.

@eerovaher eerovaher merged commit 5c2259b into astropy:main Oct 22, 2024
@neutrinoceros neutrinoceros modified the milestones: v7.1.0, v7.0.0 Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Close? Tell stale bot that this issue/PR is stale cosmology Docs no-changelog-entry-needed typing related to type annotations

Projects

Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants