Repository navigation
cosmo: annotate types & fix docs - #18546
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 |
|
@pllim Cool new label :) |
|
The |
| # =============================================================== | ||
|
|
||
| def __equiv__(self, other): | ||
| def __equiv__(self, other: Any, /) -> bool: |
There was a problem hiding this comment.
According to the docstring
| def __equiv__(self, other: Any, /) -> bool: | |
| def __equiv__(self, other: Cosmology, /) -> bool: |
There was a problem hiding this comment.
Changed to Any and updated the docs (and PR title)
There was a problem hiding this comment.
It is not obvious to me that the docstring was wrong.
There was a problem hiding this comment.
__equiv__, like __eq__, can accept any input object and returns NotImplemented if it's not compatible. Like __eq__, I've put the return type as bool only. Since __equiv__ isn't a builtin dunder method this could be adjusted...
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]>
e027c03 to
9a95cba
Compare
| def clone(self, *, meta: CosmoMeta | None = None, **kwargs: Any) -> "Cosmology": | ||
| """Returns a copy of this object with updated parameters, as specified. | ||
|
|
||
| This cannot be used to change the type of the cosmology, so ``clone()`` |
There was a problem hiding this comment.
Why is the return type not Self?
There was a problem hiding this comment.
A good Q. When a FlatCosmologyMixin is added then clone(to_nonflat=True) changes the type, so Self would be wrong.
There was a problem hiding this comment.
Right now the annotation says "Cosmology", but according to the docstring the return type is actually Self.
There was a problem hiding this comment.
The docstring still says
This cannot be used to change the type of the cosmology
so why isn't the return type Self?
There was a problem hiding this comment.
Because this isn't true for subclasses, e.g. anything that inherits from FlatCosmologyMixin.
There was a problem hiding this comment.
But then you need to update the docstring.
| # =============================================================== | ||
|
|
||
| def __equiv__(self, other): | ||
| def __equiv__(self, other: Any, /) -> bool: |
There was a problem hiding this comment.
It is not obvious to me that the docstring was wrong.
| class HasShape(Protocol): | ||
| shape: tuple[int, ...] | ||
|
|
||
|
|
||
| def aszarr( | ||
| z: Number | np.generic | HasShape | ArrayLike, | ||
| ) -> Number | np.generic | NDArray[Any]: |
There was a problem hiding this comment.
Are you trying to write down what this function is supposed to do?
There was a problem hiding this comment.
As opposed to does?
The Number | np.generic captures
astropy/astropy/cosmology/_src/utils.py
Lines 74 to 75 in 8cfd628
HasShape is for astropy/astropy/cosmology/_src/utils.py
Line 76 in 8cfd628
ArrayLike is for astropy/astropy/cosmology/_src/utils.py
Line 86 in 8cfd628
The return type is imperfect, but close enough for practical purposes.
There was a problem hiding this comment.
You should be using overloads here to distinguish scalars from arrays.
If you want to support more array libraries than just numpy then you should actually use the Array API instead of relying on hacks like hasattr(z, "shape").
There was a problem hiding this comment.
You should be using overloads here to distinguish scalars from arrays.
The plan is to move to the Array API so then scalars will be arrays.
def aszarr(x: ArrayLike) -> Array: ...If you want to support more array libraries than just numpy then you should actually use the Array API instead of relying on hacks like hasattr(z, "shape").
Older code. Eventual updates planned. HasShape is private.
There was a problem hiding this comment.
Shouldn't you use overloads to distinguish scalars from arrays? Or will Array API take care of that too?
There was a problem hiding this comment.
No need. def aszarr(x: ArrayLike) -> Array: ... will cover it all after followups.
There was a problem hiding this comment.
Though I guess maybe the overload
@overload
def aszarr[T: Array](x: T) -> T: ...
@overload
def aszarr(x: ArrayLike) -> Array: ...
would be even better. But this is all for followup.
| def deprecated_keywords(*kws, since): | ||
| def deprecated_keywords( | ||
| *kws: str, since: str | float | tuple[str | float, ...] | ||
| ) -> Callable[[Callable[P, R]], Callable[P, R]]: |
There was a problem hiding this comment.
Have you checked this with a type checker?
There was a problem hiding this comment.
Have you type checked cosmology?
There was a problem hiding this comment.
Yes. We are so far from passing 😆. Which is why I'd love to have a CI step so I could consistently target individual files to make them always pass. Running mypy on my own is not a viable long-term solution.
Co-authored-by: Eero Vaher <[email protected]>
Signed-off-by: nstarman <[email protected]>
2f26fb9 to
2d47e48
Compare
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
be7f6ad to
7704406
Compare
Just filling out some of the type annotations.