typing cosmology's io#15917
Conversation
|
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.
|
|
👋 Thank you for your draft pull request! Do you know that you can use |
cbea4f8 to
5c56583
Compare
5c56583 to
b6bf913
Compare
|
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 |
|
I'm happy to fill in as a reviewer if it helps ! |
neutrinoceros
left a comment
There was a problem hiding this comment.
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 !
eerovaher
left a comment
There was a problem hiding this comment.
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.
|
|
||
| def html_identify(origin, filepath, fileobj, *args, **kwargs): | ||
| def html_identify( | ||
| origin: Any, filepath: Any, fileobj: Any, *args: Any, **kwargs: Any |
There was a problem hiding this comment.
Why are there so many parameters that are not used?
There was a problem hiding this comment.
This is how the universal I/O identify functions are written. I suppose some formats need them.
There was a problem hiding this comment.
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.
6b697fd to
ebabffa
Compare
9a9617e to
b57931e
Compare
eerovaher
left a comment
There was a problem hiding this comment.
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.
| cosmo : `~astropy.cosmology.Cosmology` | ||
| The cosmology to return. | ||
| *args | ||
| *args : Any |
There was a problem hiding this comment.
Do you know what the NumPy docstring convention is? Is this supposed to be Any or object?
There was a problem hiding this comment.
I do not think they have specific guidance on this.
|
|
||
| def cosmology_identify(origin, format, *args, **kwargs): | ||
| def cosmology_identify( | ||
| origin: str, format: str | None, *args: Any, **kwargs: object |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
The I/O in Cosmology was based on the implementation from table.
There was a problem hiding this comment.
Could you point out the specific lines of code?
There was a problem hiding this comment.
Sure:
astropy/astropy/io/misc/hdf5.py
Line 44 in 0df2022
astropy/astropy/io/misc/parquet.py
Line 26 in 0df2022
astropy/astropy/io/votable/connect.py
Line 18 in 0df2022
There was a problem hiding this comment.
But none of these have a format parameter.
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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.
neutrinoceros
left a comment
There was a problem hiding this comment.
Sorry I missed your request for review for so long !
929c360 to
a8844ad
Compare
a8844ad to
764e772
Compare
|
Let's do #15852 first |
|
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. |
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]>
25f48da to
45a669b
Compare
|
Is this blocked any how ? I'm seeing some unresolved threads from @eerovaher's review but they're both marked as outdated. |
eerovaher
left a comment
There was a problem hiding this comment.
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.
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
Allannotations. Let's do code changes in followup PRs. I modified one docstring to get Mypy to be happy.