Skip to content
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

Merged
merged 3 commits into from
Feb 25, 2022

Conversation

maxnoe
Copy link
Member

@maxnoe maxnoe commented Feb 24, 2022

Description

  • 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")

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.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the Extra CI label.
  • Is a change log needed? If yes, did the change log check pass? If no, add the no-changelog-entry-needed label. If this is a manual backport, use the skip-changelog-checks label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but astropy-bot check might be missing; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate backport-X.Y.x label(s) before merge.

@pep8speaks
Copy link

pep8speaks commented Feb 24, 2022

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

astropy/time/core.py Outdated Show resolved Hide resolved
@maxnoe maxnoe force-pushed the time_delta_warning branch 5 times, most recently from 7721233 to cb9727a Compare February 24, 2022 14:32
@taldcroft
Copy link
Member

@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 Quantity values. It might be worth putting one example like that in Getting started.

There is a bullet point "Do time arithmetic involving Time and/or TimeDelta objects" that should also include Quantity and be backed by a sub-section with a few examples.

@maxnoe
Copy link
Member Author

maxnoe commented Feb 24, 2022

@taldcroft will do

Copy link
Contributor

@mhvk mhvk left a 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.

astropy/time/tests/test_delta.py Show resolved Hide resolved
astropy/time/core.py Outdated Show resolved Hide resolved
@maxnoe
Copy link
Member Author

maxnoe commented Feb 24, 2022

Now I am mightily confused, adding the example to the docs I get this when running `pytest doctest-glob="*.rst" docs/time/index.rst":

1234   # The now deprecated default assumes days for numeric inputs
1235   >>> t1 + 5.0
UNEXPECTED EXCEPTION: TypeError("unsupported operand type(s) for +: 'Time' and 'float'")
Traceback (most recent call last):
  File "/home/maxnoe/.local/anaconda/envs/astropy-dev/lib/python3.9/doctest.py", line 1334, in __run
    exec(compile(example.source, filename, "single",
  File "<doctest index.rst[165]>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Time' and 'float'

The same code works in python / ipython

@maxnoe maxnoe force-pushed the time_delta_warning branch 2 times, most recently from 408a355 to e74106d Compare February 24, 2022 14:53
@mhvk
Copy link
Contributor

mhvk commented Feb 24, 2022

@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 try/except at

(and the similar one at
except Exception:
), so the code returns NotImplemented, which leads python to raise a TypeError. Maybe best would be to have something like

except Exception as exc:
    if isinstance(exc, Warning):  # Not sure this is right way to know it is a warning!!!
        # Warnings are treated as exceptions; just raise.
        raise exc
    else:
        # Could not create TimeDelta, so give other a chance.
        return NotImplemented

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...

@maxnoe
Copy link
Member Author

maxnoe commented Feb 24, 2022

@mhvk I think raising is correct behavior. Someone setting -Werror wants to get the exception and not the exception turned into NotImplemented.

Edit: Just turning the bare except into except Exception should fix the issue, as warning does not inherit from BaseException and it is also not wanted that KeyboardInterrupt and SystemExit are turned into NotImplemented

@maxnoe
Copy link
Member Author

maxnoe commented Feb 24, 2022

Now I am even more confused....

>>> isinstance(Warning, Exception)
False

But except Exception catches UserWarning with -Werror.

@eerovaher
Copy link
Member

If you want to write an example in the documentation that emits a warning then you have to use the +SHOW_WARNINGS directive, see https://github.com/astropy/pytest-doctestplus#showing-warnings

@maxnoe
Copy link
Member Author

maxnoe commented Feb 24, 2022

@eerovaher Thanks, that works

@maxnoe maxnoe force-pushed the time_delta_warning branch 3 times, most recently from 5da7eaf to 5e1f92d Compare February 24, 2022 16:46
@mhvk mhvk added this to the v5.1 milestone Feb 25, 2022
Copy link
Contributor

@mhvk mhvk left a 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.

astropy/time/tests/test_delta.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mhvk mhvk left a 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!

@mhvk mhvk added 💤 merge-when-ci-passes Do not use: We have auto-merge option now. API change PRs and issues that change an existing API, possibly requiring a deprecation period labels Feb 25, 2022
Copy link
Member

@taldcroft taldcroft left a 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.

astropy/time/core.py Outdated Show resolved Hide resolved
docs/changes/time/12888.api.rst Outdated Show resolved Hide resolved
* 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")`
Copy link
Member

@taldcroft taldcroft left a 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.

@maxnoe
Copy link
Member Author

maxnoe commented Feb 25, 2022

@taldcroft If I see it correctly, there is only a fail in the allowed failures section that seems unrelated.

@maxnoe
Copy link
Member Author

maxnoe commented Feb 25, 2022

@mhvk this also closes #10355, right?

@taldcroft taldcroft merged commit 3a07771 into astropy:main Feb 25, 2022
@maxnoe maxnoe deleted the time_delta_warning branch February 25, 2022 16:00
vla22 pushed a commit to ska-telescope/katpoint that referenced this pull request Sep 7, 2022
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.
vla22 pushed a commit to ska-telescope/katpoint that referenced this pull request Sep 7, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change PRs and issues that change an existing API, possibly requiring a deprecation period modeling time 💤 merge-when-ci-passes Do not use: We have auto-merge option now.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Time allows addition with unit less quantities
5 participants