-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 warning for unitless numerical input to TimeDelta #12888
Conversation
Hello @maxnoe 👋! It looks like you've made some changes in your pull request, so I've checked the code again for style. There are no PEP8 style issues with this pull request - thanks! 🎉 Comment last updated at 2022-02-25 14:40:37 UTC |
7721233
to
cb9727a
Compare
@maxnoe - it would be good to document the default days behavior in the context of time arithmetic in the narrative docs. Looking now at the docs I see there is plenty of room for improvement. I don't see any examples of doing arithmetic with There is a bullet point "Do time arithmetic involving Time and/or TimeDelta objects" that should also include |
@taldcroft will do |
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.
@maxnoe - Looks like @taldcroft and you converged to a good solution! One suggestion for more tests, one nitpick.
Now I am mightily confused, adding the example to the docs I get this when running `pytest doctest-glob="*.rst" docs/time/index.rst":
The same code works in python / ipython |
408a355
to
e74106d
Compare
@maxnoe - I know what the issue is but am less sure how to solve it: pytest runs everything with deprecations turned into errors. But with that, it triggers the Line 2364 in 179fa5f
Line 1391 in 179fa5f
NotImplemented , which leads python to raise a TypeError . Maybe best would be to have something like
Though I'm not sure this solves the doctest issue... On the other hand, one should not ignore a warning if the user has explicitly asked for it to be treated as an exception... Which is why I'm not sure what is best... |
e74106d
to
1c88fe1
Compare
@mhvk I think raising is correct behavior. Someone setting Edit: Just turning the bare except into |
Now I am even more confused....
But |
If you want to write an example in the documentation that emits a warning then you have to use the |
1c88fe1
to
5c71389
Compare
@eerovaher Thanks, that works |
5da7eaf
to
5e1f92d
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.
Looks all good, modulo an unneeded mark in the tests, which we might as well remove.
67785e2
to
1df73b0
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.
Thanks, all good now!
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 great, thanks! Just a couple of minor suggestions inline.
I'd also request that you add an example to the top Getting started
section, in the example box just after "Finally, some further examples of what is possible. For details, see the API documentation below." Here can you put in an example like below. Otherwise this basic time arithmetic is buried way below the fold.
>>> Time('2020-01-01") + 5 * u.day
<Time object: scale='utc' format='iso' value=2020-01-06 00:00:00.000>
Finally, at this point it probably worth squashing to one commit.
* Code like `TimeDelta(5)` or `Time("2020-01-01") + 5` now raises an `AstropyDeprecationWarning`. The warning can be avoided by specifying a unit or a format explicitly: `TimeDelta(5 * u.day)`, `TimeDelta(5, format="jd")`
e725754
to
65888e2
Compare
65888e2
to
0d59d89
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.
Thanks @maxnoe! Approved with a lien on CI passing (currently in-process). I unchecked the "CI passing" box for now.
@taldcroft If I see it correctly, there is only a fail in the allowed failures section that seems unrelated. |
When creating `TimeDelta`s from a number (or combining a number with a `Time` object), Astropy assumes that the number indicates days. This long-standing behaviour has now been deprecated in Astropy 5.1, and the user will have to specify the time unit explicitly in future. See astropy/astropy#12888 for more details.
These corner cases happen when an operator goes to Astropy first: - TimeDelta + Timestamp -> TimeDelta.__add__(time_delta, ts) - Time - Timestamp -> Time.__sub__(time, ts) In both cases the katpoint.Timestamp object `ts` is cast to a TimeDelta without a format specifier, triggering the warning in Astropy>=5.1. It then proceeds to raise NotImplemented, which passes the baton to Timestamp.__radd__ and Timestamp.__rsub__, respectively, which does the right thing. Once the deprecation warning becomes a proper error (Astropy 6.0?) we can remove this workaround, as the code will just hit NotImplemented without fussing. That is, once we depend on astropy>=6.0... See astropy/astropy#12888 for more details.
Description
TimeDelta(5)
orTime("2020-01-01") + 5
now raises anAstropyDeprecationWarning
.The warning can be avoided by specifying a unit or a format explicitly:
TimeDelta(5 * u.day)
,TimeDelta(5, format="jd")
Fixes #12887
Checklist for package maintainer(s)
This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.
Extra CI
label.no-changelog-entry-needed
label. If this is a manual backport, use theskip-changelog-checks
label unless special changelog handling is necessary.astropy-bot
check might be missing; do not let the green checkmark fool you.backport-X.Y.x
label(s) before merge.