New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add field default
to Parameter
#15400
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 |
@eerovaher Let's move the discussion about |
245de6b
to
6b69640
Compare
And for comparison... I'm adding a 2nd commit with the sentinel value |
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
189c663
to
feb7940
Compare
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
Can you explain in more detail why |
If |
|
In commit 0aa86f9 I had |
One could also consider |
I think we should avoid private api. Otherwise |
The advantage of p.s. PEP 661 was interesting to read, thanks for the link! |
I like the the I think I'll also name the object Unfortunately we should not just use
from https://docs.python.org/3/library/dataclasses.html#dataclasses.field
👍 |
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
Also move extraneous test to consolidate the Parameter object tests Signed-off-by: nstarman <nstarman@users.noreply.github.com>
I changed the repr to |
|
I've long wondered at the specific design choice myself, it predating my becoming the maintainer for cosmology.
Debatably. But I would lean to yes. Particularly because I don't think just doing So my preference would be to emulate Python's |
What if the default for |
Interesting! I hadn't considered that. My hesitations are 3-fold:
|
@eerovaher, do you agree with this reasoning? This code pattern is suggested in https://peps.python.org/pep-0484/#support-for-singleton-types-in-unions for how to build statically recognizable sentinel values (until the PEP for Sentinels is in for py3.1X). |
I don't use >>> import math
>>> import numpy as np
>>> math.isnan(np.nan)
True
>>> np.isnan(math.nan)
True So in short I still don't understand why a brand new sentinel value is needed here. The reason |
They are statically different and also floats cannot be differentiated statically. I think that's important. @overload
def function(x: Literal[inf]) -> NoReturn: # Not a valid entry to `Literal`
...
@overload
def function(x: float) -> np.ndarray:
...
def function(x: float) -> np.ndarray:
if x is inf:
raise ValueError
return ... So I actually like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand now why a brand new sentinel value is needed. It would be good to use None
, but Ob0
already uses None
as its default value and the default in Parameter
needs to be different. And it is not good to use nan
because we'd like to be able to recognize the Parameter
default by type, not just by value.
Now that the big picture is cleared up I just have handful of small comments.
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great. My comments are really all nitpicks, about comments and docstrings. Approving since it is OK as is too.
def test_Parameter_default(self, cosmo_cls, all_parameter): | ||
"""Test :attr:`astropy.cosmology.Parameter.default`.""" | ||
assert hasattr(all_parameter, "default") | ||
assert isinstance( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd write this as
assert all_parameter.default is MISSING or all_parameter.default is None or isinstance(all_parameter.default, (float, u.Quantity))
This tests something closer to what is actually done (and Sentinel
is an implementation detail).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like separating out the MISSING, but NoneType is py3.10+, so there's no need to separate that out. Ruff will auto-upgrade type(None)
-> NoneType
when we next bump versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that it is clearer to do all_parameter.default is None
than isinstance(all_parameter.default, NoneType)
-- but obviously not a big deal!
Thanks @eerovaher and @mhvk for the detailed back and forth! Let's squash this in 🚀 |
Ping @eerovaher if you approve as well. |
Split off from #15168. This adds the field
default
to Parameter. Currently it doesn't do anything since the default value is specified on__init__()
, but it is useful for introspecting a cosmology class e.g.LambdaCDM.H0.default
and it will also replace__init__()
whenCosmology
is a dataclass for v6.