Skip to content

Commit

Permalink
Fix Python 3.12 deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
srittau committed Apr 29, 2024
1 parent 2595c63 commit 8ffbbb4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions asserts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
...
Expand All @@ -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))
Expand Down
18 changes: 12 additions & 6 deletions test_asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -934,28 +934,34 @@ 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$"
with assert_raises_regex(AssertionError, expected_message):
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(
None, msg_fmt="{msg};{actual!r};{now:%Y-%m-%d}"
)

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)

Expand All @@ -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)
Expand Down

0 comments on commit 8ffbbb4

Please sign in to comment.