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

Deprecation warnings are silenced while time is mocked #445

Open
tamird opened this issue Apr 30, 2024 · 3 comments · May be fixed by #486
Open

Deprecation warnings are silenced while time is mocked #445

tamird opened this issue Apr 30, 2024 · 3 comments · May be fixed by #486

Comments

@tamird
Copy link

tamird commented Apr 30, 2024

Python Version

3.12.2

pytest Version

8.1.1

Package Version

2.14.1

Description

def test_utcnow_warning() -> None:
    with time_machine.travel(0):
        datetime.utcnow() # no warning
    datetime.utcnow() # 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).
@tamird
Copy link
Author

tamird commented Apr 30, 2024

There's a further bug here which I originally (incorrectly) reported as python/typeshed#11849.

This code runs fine:

class MyDT(datetime):
    pass

my_dt = MyDT.now()
assert type(my_dt) == MyDT

This code asserts:

class MyDT(datetime):
    pass

with time_machine.travel(0):
    my_dt = MyDT.now()
assert type(my_dt) == MyDT

@adamchainz
Copy link
Owner

Ah yes, that deprecation. I am not sure what can be immediately done off the top of my head, since we deliberately avoid calling the underlying method during time travelling.

Please make a second issue for the typing difference.

@tamird
Copy link
Author

tamird commented Apr 30, 2024

Done. #446.

andersk added a commit to andersk/time-machine that referenced this issue Oct 23, 2024
Previously, when time is mocked, no DeprecationWarning would be
reported at all, and when time is not mocked, the original
datetime.utcnow would misattribute its reported DeprecationWarning to
the code of time_machine itself.  Fix both cases to attribute the
DeprecationWarning to the user code that called utcnow, by raising it
ourselves with the right stacklevel, and test that we did so
correctly.

Fixes adamchainz#445.

Signed-off-by: Anders Kaseorg <[email protected]>
andersk added a commit to andersk/time-machine that referenced this issue Oct 23, 2024
Previously, when time is mocked, no DeprecationWarning would be
reported at all, and when time is not mocked, the original
datetime.utcnow would misattribute its reported DeprecationWarning to
the code of time_machine itself.  Fix both cases to attribute the
DeprecationWarning to the user code that called utcnow, by raising it
ourselves with the right stacklevel, and test that we did so
correctly.

Fixes adamchainz#445.

Signed-off-by: Anders Kaseorg <[email protected]>
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 a pull request may close this issue.

2 participants