Skip to content

Commit c09b962

Browse files
committed
assert_datetime_about_now*: Assert dt object is naive
1 parent 044c585 commit c09b962

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

CHANGELOG.md

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

1111
### Changed
1212

13+
- Fail with `AssertionError`, not `TypeError`, if a timezone-aware datetime is
14+
passed to `assert_datetime_about_now()` or `assert_datetime_about_now_utc()`.
1315
- Modernize type annotations.
1416

1517
### Removed

asserts/__init__.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,10 @@ def assert_has_attr(obj, attribute, msg_fmt="{msg}"):
819819
_EPSILON_SECONDS = 5
820820

821821

822-
def assert_datetime_about_now(actual, msg_fmt="{msg}"):
823-
"""Fail if a datetime object is not within 5 seconds of the local time.
822+
def assert_datetime_about_now(
823+
actual: datetime | None, msg_fmt: str = "{msg}"
824+
) -> None:
825+
"""Fail if a naive datetime object is not within 5 seconds of the local time.
824826
825827
>>> assert_datetime_about_now(datetime.now())
826828
>>> assert_datetime_about_now(datetime(1900, 1, 1, 12, 0, 0))
@@ -835,18 +837,25 @@ def assert_datetime_about_now(actual, msg_fmt="{msg}"):
835837
"""
836838

837839
now = datetime.now()
840+
838841
if actual is None:
839842
msg = "None is not a valid date/time"
840843
fail(msg_fmt.format(msg=msg, actual=actual, now=now))
844+
if actual.tzinfo is not None:
845+
msg = f"expected naive datetime object, got {actual!r}"
846+
fail(msg_fmt.format(msg=msg, actual=actual, now=now))
847+
841848
lower_bound = now - timedelta(seconds=_EPSILON_SECONDS)
842849
upper_bound = now + timedelta(seconds=_EPSILON_SECONDS)
843850
if not lower_bound <= actual <= upper_bound:
844-
msg = "{!r} is not close to current date/time".format(actual)
851+
msg = f"{actual!r} is not close to current date/time"
845852
fail(msg_fmt.format(msg=msg, actual=actual, now=now))
846853

847854

848-
def assert_datetime_about_now_utc(actual, msg_fmt="{msg}"):
849-
"""Fail if a datetime object is not within 5 seconds of UTC.
855+
def assert_datetime_about_now_utc(
856+
actual: datetime | None, msg_fmt: str = "{msg}"
857+
) -> None:
858+
"""Fail if a naive datetime object is not within 5 seconds of UTC.
850859
851860
>>> assert_datetime_about_now_utc(datetime.now(timezone.utc).replace(tzinfo=None))
852861
>>> assert_datetime_about_now_utc(datetime(1900, 1, 1, 12, 0, 0))
@@ -861,13 +870,18 @@ def assert_datetime_about_now_utc(actual, msg_fmt="{msg}"):
861870
"""
862871

863872
now = datetime.now(timezone.utc).replace(tzinfo=None)
873+
864874
if actual is None:
865875
msg = "None is not a valid date/time"
866876
fail(msg_fmt.format(msg=msg, actual=actual, now=now))
877+
if actual.tzinfo is not None:
878+
msg = f"expected naive datetime object, got {actual!r}"
879+
fail(msg_fmt.format(msg=msg, actual=actual, now=now))
880+
867881
lower_bound = now - timedelta(seconds=_EPSILON_SECONDS)
868882
upper_bound = now + timedelta(seconds=_EPSILON_SECONDS)
869883
if not lower_bound <= actual <= upper_bound:
870-
msg = "{!r} is not close to current UTC date/time".format(actual)
884+
msg = f"{actual!r} is not close to current UTC date/time"
871885
fail(msg_fmt.format(msg=msg, actual=actual, now=now))
872886

873887

@@ -912,7 +926,9 @@ def __init__(self, exception: type[_E], msg_fmt: str = "{msg}") -> None:
912926
self.msg_fmt = msg_fmt
913927
self._exc_type = exception
914928
self._exc_val: _E | None = None
915-
self._exception_name: str = getattr(exception, "__name__", str(exception))
929+
self._exception_name: str = getattr(
930+
exception, "__name__", str(exception)
931+
)
916932
self._tests: list[Callable[[_E], object]] = []
917933

918934
def __enter__(self) -> Self:

test_asserts.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,11 @@ def test_assert_has_attr__does_not_have_attribute__custom_message(self):
881881

882882
# assert_datetime_about_now()
883883

884+
def test_assert_datetime_about_now__aware_dt(self):
885+
expected_message = "^expected naive datetime object, got "
886+
with assert_raises_regex(AssertionError, expected_message):
887+
assert_datetime_about_now(datetime.now(timezone.utc))
888+
884889
def test_assert_datetime_about_now__close(self):
885890
assert_datetime_about_now(datetime.now())
886891

@@ -930,6 +935,11 @@ def test_assert_datetime_about_now__custom_message(self):
930935

931936
# assert_datetime_about_now_utc()
932937

938+
def test_assert_datetime_about_now_utc__aware_dt(self):
939+
expected_message = "^expected naive datetime object, got "
940+
with assert_raises_regex(AssertionError, expected_message):
941+
assert_datetime_about_now_utc(datetime.now(timezone.utc))
942+
933943
def test_assert_datetime_about_now_utc__close(self):
934944
assert_datetime_about_now_utc(
935945
datetime.now(timezone.utc).replace(tzinfo=None)

0 commit comments

Comments
 (0)