diff --git a/CHANGELOG.md b/CHANGELOG.md index bcd5a73..1589a40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ python-asserts adheres to [semantic versioning](https://semver.org/). ## [Unreleased] +### Fixed + +Fixed Python 3.12 deprecation warnings. + ## [0.13.0] – 2024-03-13 ### Added diff --git a/asserts/__init__.py b/asserts/__init__.py index 85958d9..7edb038 100644 --- a/asserts/__init__.py +++ b/asserts/__init__.py @@ -24,7 +24,7 @@ import re import sys -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from json import loads as json_loads from typing import Any, Callable, Set from warnings import WarningMessage, catch_warnings @@ -845,7 +845,7 @@ def assert_datetime_about_now(actual, msg_fmt="{msg}"): def assert_datetime_about_now_utc(actual, msg_fmt="{msg}"): """Fail if a datetime object is not within 5 seconds of UTC. - >>> assert_datetime_about_now_utc(datetime.utcnow()) + >>> assert_datetime_about_now_utc(datetime.now(datetime.UTC)) >>> assert_datetime_about_now_utc(datetime(1900, 1, 1, 12, 0, 0)) Traceback (most recent call last): ... @@ -857,7 +857,7 @@ def assert_datetime_about_now_utc(actual, msg_fmt="{msg}"): * now - current datetime that was tested against """ - now = datetime.utcnow() + now = datetime.now(timezone.utc).replace(tzinfo=None) if actual is None: msg = "None is not a valid date/time" fail(msg_fmt.format(msg=msg, actual=actual, now=now)) diff --git a/test_asserts.py b/test_asserts.py index cb8792e..186054e 100644 --- a/test_asserts.py +++ b/test_asserts.py @@ -3,7 +3,7 @@ import re import sys from collections import OrderedDict -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from json import JSONDecodeError from unittest import TestCase from warnings import catch_warnings, simplefilter, warn @@ -934,7 +934,9 @@ def test_assert_datetime_about_now__custom_message(self): # assert_datetime_about_now_utc() def test_assert_datetime_about_now_utc__close(self): - assert_datetime_about_now_utc(datetime.utcnow()) + assert_datetime_about_now_utc( + datetime.now(timezone.utc).replace(tzinfo=None) + ) def test_assert_datetime_about_now_utc__none__default_message(self): expected_message = r"^None is not a valid date/time$" @@ -942,7 +944,7 @@ def test_assert_datetime_about_now_utc__none__default_message(self): assert_datetime_about_now_utc(None) def test_assert_datetime_about_now_utc__none__custom_message(self): - dt = datetime.utcnow().date().isoformat() + dt = datetime.now(timezone.utc).date().isoformat() expected = "None is not a valid date/time;None;{}".format(dt) with _assert_raises_assertion(expected): assert_datetime_about_now_utc( @@ -950,12 +952,16 @@ def test_assert_datetime_about_now_utc__none__custom_message(self): ) def test_assert_datetime_about_now_utc__too_low(self): - then = datetime.utcnow() - timedelta(minutes=1) + then = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta( + minutes=1 + ) with assert_raises(AssertionError): assert_datetime_about_now_utc(then) def test_assert_datetime_about_now_utc__too_high(self): - then = datetime.utcnow() + timedelta(minutes=1) + then = datetime.now(timezone.utc).replace(tzinfo=None) + timedelta( + minutes=1 + ) with assert_raises(AssertionError): assert_datetime_about_now_utc(then) @@ -970,7 +976,7 @@ def test_assert_datetime_about_now_utc__default_message(self): def test_assert_datetime_about_now_utc__custom_message(self): then = datetime(1990, 4, 13, 12, 30, 15) - now = datetime.utcnow().date().isoformat() + now = datetime.now(timezone.utc).date().isoformat() expected = ( "datetime.datetime(1990, 4, 13, 12, 30, 15) " "is not close to current UTC date/time;12:30;{}".format(now)