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
DEPR: emit a FutureWarning
when mutating Time.location
post-initialization
#16063
DEPR: emit a FutureWarning
when mutating Time.location
post-initialization
#16063
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 |
ea78f81
to
0debe8a
Compare
Time.location
51842a1
to
29a6f11
Compare
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 good! Only two small suggestions.
However, I think this is also an API change - given that we explicitly set location
in the tests, it is quite possible other people do that in their code too, so may be good to have the warning.
self._location = EarthLocation(*location) | ||
if self._location.size == 1: | ||
self._location = self._location.squeeze() | ||
elif not hasattr(self, "_location"): |
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.
Could define _location = None
on the Time
class
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 works, but I have a personal (slight) aversion for using class attributes as defaults for instance attributes, because I've been bitten by some nasty bugs that would have been easier to figure out if things were kept simpler.
I don't think this matters too much here, so let @taldcroft be the judge, and I'll go with whichever version he prefers :)
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.
@neutrinoceros - I suggested something different below, but in general I like class attribute as defaults since they more explicitly highlight the attributes of an object. This is pretty much the entire basis for dataclasses, which I've started using in new code. By using only the defined class attributes the code feels more robust. The days of tossing new attributes onto an object anywhere along the way are gone. 😄
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 dataclasses but they feel entirely different to me (small and self-contained VS large classes + inheritance trees). In general, the issue (to me) is in figuring out where attributes come from. Anyway, we digress ! I like your suggestion best, so let's try that !
t = Time("2024-02-19") | ||
|
||
with pytest.raises(AttributeError): | ||
t.location = loc |
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.
Can you also add a test where the time is initialized with a location
and one tries to change it afterwards?
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've parametrised the test to this effect !
I think this counts as an API change, so changed the label/milestone accordingly. Though perhaps good to see what @taldcroft thinks... It certainly was not the intent to be able to modify |
I'm fine with making |
@taldcroft - the question I had is whether you think that change can be backported (I felt it couldn't, but could go either way). |
29a6f11
to
123401b
Compare
I must have forgotten to undraft this. In any case, thank you @mhvk for your early review ! |
@neutrinoceros - definitely this cannot be backported. Having seen that the astropy fits tests rely on Although it doesn't impact the original hash id issue, if the Maybe one more gentle path forward is to make changing the location in the scalar case raise a future deprecation warning. This might be something like below. This should also allow reverting most or all of the changes in
|
wait. How flexible is this attribute's type anyway ?
It's less agressive to users so I like it, I'll try this now. |
123401b
to
2d8d7fb
Compare
Time.location
FutureWarning
when mutating Time.location
post-initialization
While integrating your suggestion, I found that making the warning go off only the first time the attribute is mutated required an additional layer of complexity that I don't think is warranted: the default warning filters should be enough to protect users against high noise levels, plus if we really want to be gentle about this API change, we might as well keep allowing arbitrarily numerous mutations for now. So, I'm keeping it simple for now. As a result, I also didn't revert my other changes: internal mutations are still considered allowed and should never trigger the warning, so it's probably simpler in the current state. |
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.
It looks good to me! I'm never quite sure what warning to use - should this just be AstropyDeprecationWarning
?
Experts can come in an correct me, but my rule of thumb here is that |
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.
Looks good, thanks @neutrinoceros !
AFAIK, we never (or at least seldom) use FutureWarning for deprecations in this package. I wanted to make a follow-up issue but I don't know what to follow-up on here. Do we turn this into |
Description
Fixes #16061