time-machine
time-machine copied to clipboard
Deprecation warnings are silenced while time is mocked
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).
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
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.
Done. https://github.com/adamchainz/time-machine/issues/446.