python / cpython

The Python programming language
https://www.python.org/
Other
60.96k stars 29.43k forks source link

UTC deprecation doc-string is false #120784

Open Guyw2 opened 3 weeks ago

Guyw2 commented 3 weeks ago

Documentation

(A clear and concise description of the issue.)

In the doc-string of the utcnow() function, it says:

@classmethod
    def utcnow(cls):
        "Construct a UTC datetime from time.time()."
        import warnings
        warnings.warn("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).",
                      DeprecationWarning,
                      stacklevel=2)
        t = _time.time()
        return cls._fromtimestamp(t, True, None)
 it should be 

 ```
@classmethod
def utcnow(cls):
    "Construct a UTC datetime from time.time()."
    import warnings
    warnings.warn("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(timezone.utc).",
                  DeprecationWarning,
                  stacklevel=2)
    t = _time.time()
    return cls._fromtimestamp(t, True, None)


<!-- gh-linked-prs -->
### Linked PRs
* gh-120791
<!-- /gh-linked-prs -->
picnixz commented 3 weeks ago

Well... technically datetime.UTC is also correct (it's just an alias to datetime.timezone.UTC) (here datetime.UTC is the UTC member of the datetime module, not the UTC member of the datetime.datetime class, which does not exist).

nineteendo commented 3 weeks ago

OK, but UTC isn't available on Python 3.8, 3.9 and 3.10 which are still supported:

if sys.version_info >= (3, 11):
    UTC: timezone

I believe I had to lookup timezone.utc because it wasn't suggested by my IDE.

picnixz commented 3 weeks ago

Oh my bad! then it's indeed better to use timezone.utc in the deprecation message!

nineteendo commented 3 weeks ago

Could someone fix the formatting of the issue? The link to the pull request isn't clickable.