Skip to content

Commit 8ffbbb4

Browse files
committed
Fix Python 3.12 deprecation warnings
1 parent 2595c63 commit 8ffbbb4

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ python-asserts adheres to [semantic versioning](https://semver.org/).
44

55
## [Unreleased]
66

7+
### Fixed
8+
9+
Fixed Python 3.12 deprecation warnings.
10+
711
## [0.13.0] – 2024-03-13
812

913
### Added

asserts/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import re
2626
import sys
27-
from datetime import datetime, timedelta
27+
from datetime import datetime, timedelta, timezone
2828
from json import loads as json_loads
2929
from typing import Any, Callable, Set
3030
from warnings import WarningMessage, catch_warnings
@@ -845,7 +845,7 @@ def assert_datetime_about_now(actual, msg_fmt="{msg}"):
845845
def assert_datetime_about_now_utc(actual, msg_fmt="{msg}"):
846846
"""Fail if a datetime object is not within 5 seconds of UTC.
847847
848-
>>> assert_datetime_about_now_utc(datetime.utcnow())
848+
>>> assert_datetime_about_now_utc(datetime.now(datetime.UTC))
849849
>>> assert_datetime_about_now_utc(datetime(1900, 1, 1, 12, 0, 0))
850850
Traceback (most recent call last):
851851
...
@@ -857,7 +857,7 @@ def assert_datetime_about_now_utc(actual, msg_fmt="{msg}"):
857857
* now - current datetime that was tested against
858858
"""
859859

860-
now = datetime.utcnow()
860+
now = datetime.now(timezone.utc).replace(tzinfo=None)
861861
if actual is None:
862862
msg = "None is not a valid date/time"
863863
fail(msg_fmt.format(msg=msg, actual=actual, now=now))

test_asserts.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44
import sys
55
from collections import OrderedDict
6-
from datetime import datetime, timedelta
6+
from datetime import datetime, timedelta, timezone
77
from json import JSONDecodeError
88
from unittest import TestCase
99
from warnings import catch_warnings, simplefilter, warn
@@ -934,28 +934,34 @@ def test_assert_datetime_about_now__custom_message(self):
934934
# assert_datetime_about_now_utc()
935935

936936
def test_assert_datetime_about_now_utc__close(self):
937-
assert_datetime_about_now_utc(datetime.utcnow())
937+
assert_datetime_about_now_utc(
938+
datetime.now(timezone.utc).replace(tzinfo=None)
939+
)
938940

939941
def test_assert_datetime_about_now_utc__none__default_message(self):
940942
expected_message = r"^None is not a valid date/time$"
941943
with assert_raises_regex(AssertionError, expected_message):
942944
assert_datetime_about_now_utc(None)
943945

944946
def test_assert_datetime_about_now_utc__none__custom_message(self):
945-
dt = datetime.utcnow().date().isoformat()
947+
dt = datetime.now(timezone.utc).date().isoformat()
946948
expected = "None is not a valid date/time;None;{}".format(dt)
947949
with _assert_raises_assertion(expected):
948950
assert_datetime_about_now_utc(
949951
None, msg_fmt="{msg};{actual!r};{now:%Y-%m-%d}"
950952
)
951953

952954
def test_assert_datetime_about_now_utc__too_low(self):
953-
then = datetime.utcnow() - timedelta(minutes=1)
955+
then = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(
956+
minutes=1
957+
)
954958
with assert_raises(AssertionError):
955959
assert_datetime_about_now_utc(then)
956960

957961
def test_assert_datetime_about_now_utc__too_high(self):
958-
then = datetime.utcnow() + timedelta(minutes=1)
962+
then = datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(
963+
minutes=1
964+
)
959965
with assert_raises(AssertionError):
960966
assert_datetime_about_now_utc(then)
961967

@@ -970,7 +976,7 @@ def test_assert_datetime_about_now_utc__default_message(self):
970976

971977
def test_assert_datetime_about_now_utc__custom_message(self):
972978
then = datetime(1990, 4, 13, 12, 30, 15)
973-
now = datetime.utcnow().date().isoformat()
979+
now = datetime.now(timezone.utc).date().isoformat()
974980
expected = (
975981
"datetime.datetime(1990, 4, 13, 12, 30, 15) "
976982
"is not close to current UTC date/time;12:30;{}".format(now)

0 commit comments

Comments
 (0)