Skip to content

Commit 448923a

Browse files
fix: replace deprecated datetime.datetime.utcnow() (#552)
Fixes #540 Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 83fa302 commit 448923a

File tree

6 files changed

+8
-12
lines changed

6 files changed

+8
-12
lines changed

google/api_core/datetime_helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

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

4747

4848
def to_milliseconds(value):

noxfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def default(session, install_grpc=True):
143143
session.run(*pytest_args)
144144

145145

146-
@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11"])
146+
@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"])
147147
def unit(session):
148148
"""Run the unit test suite."""
149149
default(session)

testing/constraints-3.12.txt

Whitespace-only changes.

tests/asyncio/test_retry_async.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ async def test___call___and_execute_success(self, sleep):
280280
@mock.patch("asyncio.sleep", autospec=True)
281281
@pytest.mark.asyncio
282282
async def test___call___and_execute_retry(self, sleep, uniform):
283-
284283
on_error = mock.Mock(spec=["__call__"], side_effect=[None])
285284
retry_ = retry_async.AsyncRetry(
286285
predicate=retry_async.if_exception_type(ValueError)
@@ -305,7 +304,6 @@ async def test___call___and_execute_retry(self, sleep, uniform):
305304
@mock.patch("asyncio.sleep", autospec=True)
306305
@pytest.mark.asyncio
307306
async def test___call___and_execute_retry_hitting_deadline(self, sleep, uniform):
308-
309307
on_error = mock.Mock(spec=["__call__"], side_effect=[None] * 10)
310308
retry_ = retry_async.AsyncRetry(
311309
predicate=retry_async.if_exception_type(ValueError),
@@ -315,7 +313,7 @@ async def test___call___and_execute_retry_hitting_deadline(self, sleep, uniform)
315313
deadline=9.9,
316314
)
317315

318-
utcnow = datetime.datetime.utcnow()
316+
utcnow = datetime.datetime.now(tz=datetime.timezone.utc)
319317
utcnow_patcher = mock.patch(
320318
"google.api_core.datetime_helpers.utcnow", return_value=utcnow
321319
)

tests/unit/test_retry.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ def test___call___and_execute_success(self, sleep):
361361
@mock.patch("random.uniform", autospec=True, side_effect=lambda m, n: n)
362362
@mock.patch("time.sleep", autospec=True)
363363
def test___call___and_execute_retry(self, sleep, uniform):
364-
365364
on_error = mock.Mock(spec=["__call__"], side_effect=[None])
366365
retry_ = retry.Retry(predicate=retry.if_exception_type(ValueError))
367366

@@ -383,7 +382,6 @@ def test___call___and_execute_retry(self, sleep, uniform):
383382
@mock.patch("random.uniform", autospec=True, side_effect=lambda m, n: n)
384383
@mock.patch("time.sleep", autospec=True)
385384
def test___call___and_execute_retry_hitting_deadline(self, sleep, uniform):
386-
387385
on_error = mock.Mock(spec=["__call__"], side_effect=[None] * 10)
388386
retry_ = retry.Retry(
389387
predicate=retry.if_exception_type(ValueError),
@@ -393,7 +391,7 @@ def test___call___and_execute_retry_hitting_deadline(self, sleep, uniform):
393391
deadline=30.9,
394392
)
395393

396-
utcnow = datetime.datetime.utcnow()
394+
utcnow = datetime.datetime.now(tz=datetime.timezone.utc)
397395
utcnow_patcher = mock.patch(
398396
"google.api_core.datetime_helpers.utcnow", return_value=utcnow
399397
)

tests/unit/test_timeout.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def test___str__(self):
5858
def test_apply(self):
5959
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
6060

61-
datetime.datetime.utcnow()
61+
datetime.datetime.now(tz=datetime.timezone.utc)
6262
datetime.timedelta(seconds=1)
6363

64-
now = datetime.datetime.utcnow()
64+
now = datetime.datetime.now(tz=datetime.timezone.utc)
6565

6666
times = [
6767
now,
@@ -92,10 +92,10 @@ def _clock():
9292
def test_apply_no_timeout(self):
9393
target = mock.Mock(spec=["__call__", "__name__"], __name__="target")
9494

95-
datetime.datetime.utcnow()
95+
datetime.datetime.now(tz=datetime.timezone.utc)
9696
datetime.timedelta(seconds=1)
9797

98-
now = datetime.datetime.utcnow()
98+
now = datetime.datetime.now(tz=datetime.timezone.utc)
9999

100100
times = [
101101
now,

0 commit comments

Comments
 (0)