@@ -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 :
0 commit comments