Skip to content

Commit

Permalink
fix: replace deprecated datetime.datetime.utcnow() (#552)
Browse files Browse the repository at this point in the history
Fixes #540

Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
DouglasBlackwood and parthea committed Nov 17, 2023
1 parent 83fa302 commit 448923a
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion google/api_core/datetime_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

def utcnow():
"""A :meth:`datetime.datetime.utcnow()` alias to allow mocking in tests."""
return datetime.datetime.utcnow()
return datetime.datetime.now(tz=datetime.timezone.utc).replace(tzinfo=None)


def to_milliseconds(value):
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def default(session, install_grpc=True):
session.run(*pytest_args)


@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11"])
@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"])
def unit(session):
"""Run the unit test suite."""
default(session)
Expand Down
Empty file added testing/constraints-3.12.txt
Empty file.
4 changes: 1 addition & 3 deletions tests/asyncio/test_retry_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ async def test___call___and_execute_success(self, sleep):
@mock.patch("asyncio.sleep", autospec=True)
@pytest.mark.asyncio
async def test___call___and_execute_retry(self, sleep, uniform):

on_error = mock.Mock(spec=["__call__"], side_effect=[None])
retry_ = retry_async.AsyncRetry(
predicate=retry_async.if_exception_type(ValueError)
Expand All @@ -305,7 +304,6 @@ async def test___call___and_execute_retry(self, sleep, uniform):
@mock.patch("asyncio.sleep", autospec=True)
@pytest.mark.asyncio
async def test___call___and_execute_retry_hitting_deadline(self, sleep, uniform):

on_error = mock.Mock(spec=["__call__"], side_effect=[None] * 10)
retry_ = retry_async.AsyncRetry(
predicate=retry_async.if_exception_type(ValueError),
Expand All @@ -315,7 +313,7 @@ async def test___call___and_execute_retry_hitting_deadline(self, sleep, uniform)
deadline=9.9,
)

utcnow = datetime.datetime.utcnow()
utcnow = datetime.datetime.now(tz=datetime.timezone.utc)
utcnow_patcher = mock.patch(
"google.api_core.datetime_helpers.utcnow", return_value=utcnow
)
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ def test___call___and_execute_success(self, sleep):
@mock.patch("random.uniform", autospec=True, side_effect=lambda m, n: n)
@mock.patch("time.sleep", autospec=True)
def test___call___and_execute_retry(self, sleep, uniform):

on_error = mock.Mock(spec=["__call__"], side_effect=[None])
retry_ = retry.Retry(predicate=retry.if_exception_type(ValueError))

Expand All @@ -383,7 +382,6 @@ def test___call___and_execute_retry(self, sleep, uniform):
@mock.patch("random.uniform", autospec=True, side_effect=lambda m, n: n)
@mock.patch("time.sleep", autospec=True)
def test___call___and_execute_retry_hitting_deadline(self, sleep, uniform):

on_error = mock.Mock(spec=["__call__"], side_effect=[None] * 10)
retry_ = retry.Retry(
predicate=retry.if_exception_type(ValueError),
Expand All @@ -393,7 +391,7 @@ def test___call___and_execute_retry_hitting_deadline(self, sleep, uniform):
deadline=30.9,
)

utcnow = datetime.datetime.utcnow()
utcnow = datetime.datetime.now(tz=datetime.timezone.utc)
utcnow_patcher = mock.patch(
"google.api_core.datetime_helpers.utcnow", return_value=utcnow
)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def test___str__(self):
def test_apply(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")

datetime.datetime.utcnow()
datetime.datetime.now(tz=datetime.timezone.utc)
datetime.timedelta(seconds=1)

now = datetime.datetime.utcnow()
now = datetime.datetime.now(tz=datetime.timezone.utc)

times = [
now,
Expand Down Expand Up @@ -92,10 +92,10 @@ def _clock():
def test_apply_no_timeout(self):
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")

datetime.datetime.utcnow()
datetime.datetime.now(tz=datetime.timezone.utc)
datetime.timedelta(seconds=1)

now = datetime.datetime.utcnow()
now = datetime.datetime.now(tz=datetime.timezone.utc)

times = [
now,
Expand Down

0 comments on commit 448923a

Please sign in to comment.