-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
GH-103857: Deprecate utcnow and utcfromtimestamp #103858
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (fixing the remaining test failures seems straight-forward.)
6294fc7
to
66a82a0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this, my nit is about the wordiness of the warnings.
Outside of that, there's the question of mixing tz-aware and tz-naive datetimes when it comes to comparisons and arithmetic operations. It might be somewhat disruptive for users to replace one with the other to discover TypeErrors in their code due to mixing naive and tz-aware datetimes.
Lib/datetime.py
Outdated
warnings.warn("datetime.utcfromtimestamp() is deprecated and scheduled " | ||
"for removal in a future version. It is instead " | ||
"recommended that users use timezone-aware objects to " | ||
"represent datetimes in UTC. To get such an object from " | ||
"a timestamp, use datetime.fromtimestamp(t, datetime.UTC).", | ||
DeprecationWarning, | ||
stacklevel=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this a little wordy for a warning, and tentative. The deprecated function will be removed in the future so we are not recommending an alternative, we are telling people to use an alternative. How about this:
warnings.warn("datetime.utcfromtimestamp() is deprecated and scheduled " | |
"for removal in a future version. It is instead " | |
"recommended that users use timezone-aware objects to " | |
"represent datetimes in UTC. To get such an object from " | |
"a timestamp, use datetime.fromtimestamp(t, datetime.UTC).", | |
DeprecationWarning, | |
stacklevel=2) | |
warnings.warn("datetime.utcfromtimestamp() is deprecated and will be " | |
"removed in a future version. Use timezone-aware objects to " | |
"represent datetimes in UTC: " | |
"datetime.fromtimestamp(t, datetime.UTC)", | |
DeprecationWarning, | |
stacklevel=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did think it was a bit wordy when I wrote it. My only reservation here is that users still can use naïve objects as they want, they just need to do datetime.now(UTC).replace(tzinfo=None)
.
I suppose this subtle distinction is probably going to be lost on most people, so I'll just go with your wording.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that Łukasz's suggestion is an improvement. I think it is ok to suggest the slightly disruptive change; we want people to use tz-aware objects
I mean, in either case we're suggesting the change, the question is how best to communicate to users that the thing we're suggesting is not a drop-in replacement for the existing workflow. I tend to assume that when I see a deprecation warning with a code snippet in it, the code snippet is how you achieve the same thing in the new version. Ideally we'd make it clear that we are saying, "The thing you are doing now is not something you should continue doing, here's a new way to do it but you need to understand the implications of making this change."
That said, I'm not convinced that it's possible to convey this succinctly, so probably best to just leave it at Łukasz's wording.
Lib/datetime.py
Outdated
warnings.warn("datetime.utcnow() is deprecated and scheduled for " | ||
"removal in a future version. It is instead recommended " | ||
"that users use timezone-aware objects to represent " | ||
"datetimes in UTC. To get such an object representing " | ||
"the current time, use datetime.now(datetime.UTC).", | ||
DeprecationWarning, | ||
stacklevel=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
warnings.warn("datetime.utcnow() is deprecated and scheduled for " | |
"removal in a future version. It is instead recommended " | |
"that users use timezone-aware objects to represent " | |
"datetimes in UTC. To get such an object representing " | |
"the current time, use datetime.now(datetime.UTC).", | |
DeprecationWarning, | |
stacklevel=2) | |
warnings.warn("datetime.utcnow() is deprecated and will be removed " | |
"in a future version. Use timezone-aware objects to " | |
"represent datetimes in UTC: datetime.now(datetime.UTC)", | |
DeprecationWarning, | |
stacklevel=2) |
Modules/_datetimemodule.c
Outdated
PyErr_WarnEx( | ||
PyExc_DeprecationWarning, | ||
"datetime.utcnow() is deprecated and scheduled for removal in a future " | ||
"version. It is instead recommended that users use timezone-aware " | ||
"objects to represent datetimes in UTC. To get such an object " | ||
"representing the current time, use datetime.now(datetime.UTC).", | ||
2 | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here about replacing the warning with the shorter version.
Modules/_datetimemodule.c
Outdated
PyErr_WarnEx( | ||
PyExc_DeprecationWarning, | ||
"datetime.utcfromtimestamp() is deprecated and scheduled for removal " | ||
"in a future version. It is instead recommended that users use " | ||
"timezone-aware objects to represent datetimes in UTC. To get such an " | ||
"object from a timestamp, use datetime.fromtimestamp(t, datetime.UTC).", | ||
2 | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
Yes, this was somewhat hard to convey in the wording here. I am not trying to suggest that Another option is to just say, "Use timezone-aware datetimes instead" and not include the code for how to do that, so that they are forced to look up how to do that and are less likely to think it's a drop-in replacement? I suspect that doing so would be a somewhat frustrating user expreience, though. Tough call. |
@ambv (or others), what do you think of this wording?
Alternatively, This will not be a backwards-compatible change. or something to that effect? |
66a82a0
to
3c7c703
Compare
688bb05
to
b223173
Compare
OK, I'm going to merge this when CI passes, using Łukasz's wording, unless there are some objections. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Per the reasoning in #103857.
To do:
utcnow
andutcfromtimestamp
utcnow
andutcfromtimestamp
#103857