Skip to content

Preventing the Python 3.12's "Deprecated since version 3.12: Use datetime.now() with UTC instead." #914

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Lisias
Copy link

@Lisias Lisias commented Aug 18, 2024

Description

See #913

Types of changes

  • Replay fix (fixes a replay specific issue)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added or updated tests to cover my changes.
  • All new and existing tests passed.

@Lisias Lisias marked this pull request as draft August 18, 2024 06:04
@Lisias
Copy link
Author

Lisias commented Aug 27, 2024

I made a mistake, forgot that WARCIO is also using naive datetimes. I'm working on a short term solution, please wait.

@Lisias
Copy link
Author

Lisias commented Mar 17, 2025

I finally got my stuff together and remembered this one.

I just realized that WARCIO was updated:

webrecorder/warcio#185

(yeah, I'm a bit slow... 😬 )

I'm revising this code and comparing it with whatever I have something in (my custom) production at this moment - that code is working for months already, time more than enough to had bitten my back if I had made some serious mistake.

@Lisias Lisias force-pushed the pullquests/to/upstream/issue-913 branch from 7708930 to 1b78bb4 Compare March 17, 2025 08:08
@Lisias
Copy link
Author

Lisias commented Mar 17, 2025

That's the history: With WARCIO 1.7.5 giving us the option for getting non naive datetimes from it, after this pull request is applied we would have internally code using both datetime types: the deprecating naive ones, and the new canon tz aware ones.

This would be a problem on code touched by the previous commit, and so code reviewing the changes I found that pywb.warcserver.access_checker and pywb.warcserver.resource.responseloader would end up handling both datetimes concurrently - what could lead to undesired surprises on the long run.

So I changed these, adding True to the calls to WARCIO's timestamp_to_datetime at:

Doing a second code review, this time worrying about the consequences of the changes, I realized that these changes are not needed because the datetime generated are fed back into WARCIO and then forgot about - and so there's no possible clash between naive and tz aware datetimes - and that last commit of mine is useless.

However, I had forgot that updating my branch would reflect automatically on this pull request, my bad. So I decided to rebase the branch back one commit, and ask first if you guys would prefer to have the code above changed due some internal compliance, or if whatever we have now is good to go.

I'm marking this pull request as Ready for Review anyway - as I found no possible problem with WARCIO with or without getting naive datetimes from them.

--- edit: some typos fixed.

@Lisias Lisias marked this pull request as ready for review March 17, 2025 08:20
Copy link
Member

@tw4l tw4l left a comment

Choose a reason for hiding this comment

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

Thank you for this PR, and apologies for our taking a while to review it! I think there are two cases where we explicitly need to create naive datetimes still to avoid comparison issues.

@@ -164,13 +164,13 @@ def check_embargo(self, url, ts):
# embargo if newser than
newer = self.embargo.get('newer')
if newer:
actual = datetime.utcnow() - newer
actual = datetime.now(timezone.utc) - newer
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
actual = datetime.now(timezone.utc) - newer
actual = datetime.now(timezone.utc).replace(tzinfo=None) - newer

I think this is one of the few places where we do need to explicitly create a naive datetime or else an exception will be thrown due to comparison of naive and aware datetimes.

Copy link

Choose a reason for hiding this comment

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

newer is a relativedelta, not a datetime.

return access if actual < dt else None

# embargo if older than
older = self.embargo.get('older')
if older:
actual = datetime.utcnow() - older
actual = datetime.now(timezone.utc) - older
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
actual = datetime.now(timezone.utc) - older
actual = datetime.now(timezone.utc).replace(tzinfo=None) - newer

Same as above.

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.

4 participants