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

Fix datetime.utcnow() deprecation #134

Merged
merged 4 commits into from
Jan 18, 2024

Conversation

jdavies-st
Copy link
Contributor

This fixes the following deprecation warning:

DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC)

Note that when you pass any timezone to datetime.now(), it returns the offset from UTC (format +HH:MM) appended to the string. The offset from UTC is always zero, and datetime.utcnow() didn't include it. So here it is removed so that timestamps have a consistent format.

@jdavies-st jdavies-st requested a review from a team as a code owner January 18, 2024 11:00
Copy link

codecov bot commented Jan 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (9e7c441) 59.15% compared to head (8b120a0) 59.15%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #134   +/-   ##
=======================================
  Coverage   59.15%   59.15%           
=======================================
  Files          24       24           
  Lines        1611     1611           
=======================================
  Hits          953      953           
  Misses        658      658           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@braingram braingram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this update! I made a suggestion that should work with python 3.9 to fix the CI.

src/stpipe/config.py Outdated Show resolved Hide resolved
@@ -111,7 +111,10 @@ def to_asdf(self, include_metadata=False):
meta = deepcopy(_META_TEMPLATE)
meta["date"] = meta["date"].replace(
_TEMPLATE_PLACEHOLDER,
datetime.utcnow().replace(microsecond=0).isoformat(),
datetime.now(UTC)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
datetime.now(UTC)
datetime.now(timezone.utc)

I believe this and the other suggestion should allow the CI to work with 3.9 and matches the changes made in jwst.

Although not necessary with this PR, would you add a python 3.12 environment to tox and the CI? This would be great to verify that the DeprecationWarning is fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Done.

This actually doesn't get picked up in the stpipe unit tests, but does in the jwst unit tests, which test config_parser.py better than stpipe does. One might want to think about adding coverage to the downstream tests, specifically jwst.

Copy link
Collaborator

@braingram braingram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I totally missed that the lines weren't covered by the unit tests. It might be worth moving some of those over (if that's possible) but that's a problem for another day.

LGTM.

@jdavies-st
Copy link
Contributor Author

jdavies-st commented Jan 18, 2024

Agree that it would be nice if this package could self-test better. Another day.

I will note that by putting coverage on the downstream test workflows and adding codecov flags, you can keep track separately of which coverage is coming from the package unit tests and how well the downstream packages test it. But currently the OpenAstronomy CI workflows don't support specifying codecov flags, though it would be easy enough to add probably. Flags are used in the jwst package Jenkins regtests currently, though the unit test flags disappeared with the switch to OpenAstronomy CI workflows.

@braingram braingram merged commit bb73636 into spacetelescope:master Jan 18, 2024
16 checks passed
@jdavies-st jdavies-st deleted the fix-datetime-deprecation branch January 18, 2024 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants