FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-103857: Update deprecation stacktrace to point to calling line by hugovk · Pull Request #104431 · python/cpython · GitHub

/ cpython Public

gh-103857: Update deprecation stacktrace to point to calling line - #104431

Merged
abalkin merged 2 commits into
python:mainfrom
hugovk:datetime-deprecations-stacktrace
May 12, 2023
Merged

gh-103857: Update deprecation stacktrace to point to calling line#104431
abalkin merged 2 commits into
python:mainfrom
hugovk:datetime-deprecations-stacktrace

Conversation

hugovk commented May 12, 2023
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

Set up

# From CPython repo:
git clone https://github.com/hugovk/pepotron ../pepotron
./python.exe -m pip install "../pepotron[tests]"

Before

Warnings don't point to the actual lines of code that calls the deprecated utcfromtimestamp and utcnow:

$ ./python.exe -m pytest ../pepotron
...
==================================================================== warnings summary =====================================================================
../../.local/lib/python3.12/site-packages/dateutil/tz/__init__.py:2
  /Users/hugo/.local/lib/python3.12/site-packages/dateutil/tz/__init__.py:2: DeprecationWarning: datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).
    from .tz import *

tests/test_pepotron.py::test_url[dead batteries-https://peps.python.org/pep-0594/]
tests/test_pepotron.py::test_url_base_url[dead batteries-https://hugovk.github.io/peps-https://hugovk.github.io/peps/pep-0594/]
  /Users/hugo/.local/lib/python3.12/site-packages/pepotron/__init__.py:54: DeprecationWarning: datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).
    cache_file = _cache.filename(json_url)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
...

After

Now points to the actual lines of code directly calling utcfromtimestamp and utcnow:

$ $ ./python.exe -m pytest ../pepotron
...
==================================================================== warnings summary =====================================================================
../../.local/lib/python3.12/site-packages/dateutil/tz/tz.py:37
  /Users/hugo/.local/lib/python3.12/site-packages/dateutil/tz/tz.py:37: DeprecationWarning: datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).
    EPOCH = datetime.datetime.utcfromtimestamp(0)

tests/test_pepotron.py::test_url[dead batteries-https://peps.python.org/pep-0594/]
tests/test_pepotron.py::test_url_base_url[dead batteries-https://hugovk.github.io/peps-https://hugovk.github.io/peps/pep-0594/]
  /Users/hugo/.local/lib/python3.12/site-packages/pepotron/_cache.py:30: DeprecationWarning: datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).
    today = dt.datetime.utcnow().strftime("%Y-%m-%d")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
...

pganssle commented May 12, 2023
edited
Loading

Copy link
Copy Markdown
Member

I think _pydatetime.py presumably needs to be updated as well.

abalkin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks right.

abalkin commented May 12, 2023
edited
Loading

Copy link
Copy Markdown
Member

I think _pydatetime.py presumably needs to be updated as well.

The stacklevel setting in the pure python implementation is actually correct because warning is generated inside a python,rather than a C function in this case:

    def utcfromtimestamp(cls, t):
        """Construct a naive UTC datetime from a POSIX timestamp."""
        import warnings
        warnings.warn("datetime.utcfromtimestamp() is deprecated and scheduled "
                      "for removal in a future version. Use timezone-aware "
                      "objects to represent datetimes in UTC: "
                      "datetime.fromtimestamp(t, datetime.UTC).",
                      DeprecationWarning,
                      stacklevel=2)
        return cls._fromtimestamp(t, True, None)

To test, create the following file:

cat > test_103857.py
import sys
sys.modules['_datetime'] = None
from datetime import datetime

def f():
    return datetime.utcfromtimestamp(0)

f()

and run

% ./python.exe -W always test_103857.py
/Users/a/Work/abalkin/cpython-1/test_103857.py:6: DeprecationWarning: datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.fromtimestamp(t, datetime.UTC).
  return datetime.utcfromtimestamp(0)

hugovk deleted the datetime-deprecations-stacktrace branch May 13, 2023 14:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL