time-machine icon indicating copy to clipboard operation
time-machine copied to clipboard

Deprecation warnings are silenced while time is mocked

Open tamird opened this issue 1 year ago • 3 comments

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 avatar Apr 30 '24 18:04 tamird

There's a further bug here which I originally (incorrectly) reported as https://github.com/python/typeshed/issues/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

tamird avatar Apr 30 '24 20:04 tamird

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.

adamchainz avatar Apr 30 '24 20:04 adamchainz

Done. https://github.com/adamchainz/time-machine/issues/446.

tamird avatar Apr 30 '24 21:04 tamird