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

DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version #163

Open
benoit74 opened this issue Mar 1, 2024 · 7 comments

Comments

@benoit74
Copy link

benoit74 commented Mar 1, 2024

Since Python 3.12, we have the following DeprecationWarning:

warcio/recordbuilder.py:156: 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).
    return datetime_to_iso_date(datetime.datetime.utcnow(), use_micros=use_micros)

Pretty easy to fix in 3.12, but maybe not that easy in reality since you probably wanna maintain 2.7+ and 3.4+ support. I suggest to not use what the DeprecationWarning suggests but datetime.datetime.now(tzinfo = datetime.tzinfo.UTC) which should be OK (to be checked).

@benoit74
Copy link
Author

I tried to work on this feature by first adding support for Python 3.12 but I fail to get the tests running with it.

First problem is in test_capture_http_proxy.py:

  • werkzeug dependency must be pinned to 2.0.3 for working with httpbin==0.5.0
  • setup(cls) should be replaced by setup_class(cls)
  • requests complains that proxies set are not a valid URL (we should probably add the "http://" prefix manually)
  • but even after that the HTTPS tests are still failing
  • upgrading to latest httpbin / werkzeug version does not help

I don't know how to fix this situation

@wumpus
Copy link
Collaborator

wumpus commented May 23, 2024

Tessa @tw4l is working on getting rid of the httpbin version dependency in PR 153 #153 -- and she's setting up Github Actions so we'll have CI again. Once that's done you'll easily be able to finish this one.

@benoit74
Copy link
Author

Oh great, thank you! I'm glad I stopped before investing too much time in this ^^

@tw4l
Copy link
Contributor

tw4l commented May 24, 2024

I'm struggling with the same HTTPS proxy issue as you document above, but hopefully will work it out soon!

@benoit74
Copy link
Author

Good luck! (I have my own share of struggling, I know what this is ^^)

@tw4l
Copy link
Contributor

tw4l commented May 24, 2024

Turns out pinning urllib3 to an older version for now resolves it! PR to switch to GitHub Actions CI is now open :) #164

@Lisias
Copy link

Lisias commented Aug 27, 2024

datetime.datetime.now(tzinfo = datetime.tzinfo.UTC) which should be OK (to be checked).

Nope, it will trigger exceptions while doing math with deltatime for code that was relying on the previous (current) behaviour of return naïve datetime, as in warcio.timeutils.timestamp_to_datetime.

Got bitten by it on a custom Filter I wrote on pywb.

My current proposal is to revise all warcio functions returning a datetime and do something like:

def timestamp_to_datetime(string, tzinfo:datetime.timezone=None) -> datetime.datetime:
#                                ^^^^^^^ HERE ! ^^^^^^^^^^^^^^^^^
	#
	# yada yada yada
	#
	return datetime.datetime(year=year,
                                                         month=month,
                                                         day=day,
                                                         hour=hour,
                                                         minute=minute,
                                                         second=second,
                                                         tzinfo=tzinfo) # <---- HERE!!!!

This way, all current code will still get the expected behaviour, but users willing to cope with >= 3.12 can just add datetime.timezone.utc to the call and get an aware datetime instead.

Eventually the naive datetime will be fully deprecated in some future Python version. When this happens, changing the function signature to

def timestamp_to_datetime(string, tzinfo:datetime.timezone=datetime.timezone.utc) -> datetime.datetime:

Will "automagically" convert all callers to aware datetime. Code that already coped with the deprecation will not be affected, and code that will break will break in the client's land where the fix will be obvious, and not with some Exception inside warcio that will prompt the client to seek for support.

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

No branches or pull requests

4 participants