-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
utcnow
deprecation note is misleading
#118542
Comments
It was definitely a deliberate decision to avoid suggesting a drop-in replacement. I had thought that the wording also made it clear that making this change would be a brain change to your interfaces, though. The reason to avoid suggesting a drop-in replacement is because the main issue with |
It's not that I disagree, but the current wording doesn't make the impact of the change clear. Changing a naïve datetime to a tz aware one is a major change and very likely to break user code if not done carefully. The situation is actually quite comparable to the Python 2 Also, at the moment, naïve datetimes are not deprecated as far as I'm aware. And while that may be a worthwhile goal, I think this would require a PEP, considering the impact of that change. And as long as it's not deprecated I don't think we should force users into a transition they might not be interested in. |
I am not saying that the message can't be improved, just that we should not be offering a drop-in replacement method. Users need to think about how to make the change, and if we offer a "no thought required" option they will take it.
Correct, they are not deprecated, and indeed it would not be a worthwhile goal. My suggestion is not to eliminate naïve datetimes, but to stop people from using them wrong. If there were a method in the standard library that attached |
Very often the change will be "I still want a naïve datetime object", either to guarantee a stable API, or to easily upgrade to Python 3.12, without the additional hassle of updating the usage of datetime objects throughout the code. Especially since naïve datetime objects are not deprecated and will continue to work for the foreseeable future. Having users to jump through additional hoops to fulfill their goal, and offering only the dangerous solution in the documentation, instead of enabling users to make an informed decision themselves sounds quite patronising, to be honest. If users will take the "easy solution" that's their prerogative. |
Have you considered just dropping the descriptions of utcnow and utcfromtimestamp, and replacing them with something like datetime.utcnow() Deprecated since 3.12. datetime.utcfromtimestamp(timestamp) Deprecated since 3.12. Perhaps also rescue the description of fromtimestamp(. . ., timezone.utc) which doesn’t really belong under utcfromtimestamp, and remove the opening claim suggesting fromtimestamp’s date and time is always local. |
Documentation
Currently, the deprecation notice for
datetime.utcnow()
reads:Unfortunately, applying this suggestion verbatim can easily break user code – especially when applying it to libraries. (See for example https://foss.heptapod.net/openpyxl/openpyxl/-/issues/2051). These datetime-related TypeErrors are easy to introduce, and fairly hard to fix. I suggest to amend the deprecation notice:
Maybe someone can come up with a better wording.
Linked PRs
The text was updated successfully, but these errors were encountered: