Skip to content

Commit

Permalink
Fix datetime.utcnow deprecation in Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
sjagoe committed Dec 5, 2023
1 parent 7ecefd7 commit 59809b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 13 additions & 3 deletions haas/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
from .error_holder import ErrorHolder


if sys.version_info >= (3, 12):
from datetime import UTC

def datetime_utcnow():
return datetime.now(UTC)
else:
def datetime_utcnow():
return datetime.utcnow()


class TestCompletionStatus(Enum):
"""Enumeration to represent the status of a single test.
Expand Down Expand Up @@ -422,7 +432,7 @@ def startTest(self, test, start_time=None):
"""
if start_time is None:
start_time = datetime.utcnow()
start_time = datetime_utcnow()
self._test_timing[self._testcase_to_key(test)] = start_time
self._mirror_output = False
self._setup_stdout()
Expand Down Expand Up @@ -496,13 +506,13 @@ def _handle_result(self, test, status, exception=None, message=None):

started_time = self._test_timing.get(self._testcase_to_key(test))
if started_time is None and isinstance(test, ErrorHolder):
started_time = datetime.utcnow()
started_time = datetime_utcnow()
elif started_time is None:
raise RuntimeError(
'Missing test start! Please report this error as a bug in '
'haas.')

completion_time = datetime.utcnow()
completion_time = datetime_utcnow()
duration = TestDuration(started_time, completion_time)
result = TestResult.from_test_case(
test,
Expand Down
3 changes: 3 additions & 0 deletions haas/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ def utcnow(self):
return next(self.ret)
except StopIteration:
raise ValueError('No more mock values!')

def now(self, tz):
return self.utcnow()

0 comments on commit 59809b1

Please sign in to comment.