Skip to content

Commit 5805066

Browse files
refactor: Fix DeprecationWarnings for datetime methods in job tests (#2185)
* Fix DeprecationWarnings for datetime methods in job tests Replaced calls to deprecated `datetime.datetime.utcnow()` with `datetime.datetime.now(datetime.UTC)` in `tests/unit/job/test_base.py`. Replaced calls to deprecated `datetime.datetime.utcfromtimestamp()` with `datetime.datetime.fromtimestamp(timestamp, datetime.UTC)` in `tests/unit/job/helpers.py`. These changes address the specific warnings identified in the issue for these two files. * Update tests/unit/job/test_base.py * Update tests/unit/job/test_base.py * Updates datetime code related to UTC --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 110ad60 commit 5805066

File tree

7 files changed

+31
-23
lines changed

7 files changed

+31
-23
lines changed

tests/unit/job/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def _setUpConstants(self):
106106
from google.cloud._helpers import UTC
107107

108108
self.WHEN_TS = 1437767599.006
109-
self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC)
109+
self.WHEN = datetime.datetime.fromtimestamp(self.WHEN_TS, UTC).replace(
110+
tzinfo=UTC
111+
)
110112
self.ETAG = "ETAG"
111113
self.FULL_JOB_ID = "%s:%s" % (self.PROJECT, self.JOB_ID)
112114
self.RESOURCE_URL = "{}/bigquery/v2/projects/{}/jobs/{}".format(

tests/unit/job/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def _datetime_and_millis():
331331
import datetime
332332
from google.cloud._helpers import _millis
333333

334-
now = datetime.datetime.utcnow().replace(
334+
now = datetime.datetime.now(datetime.timezone.utc).replace(
335335
microsecond=123000,
336336
tzinfo=datetime.timezone.utc, # stats timestamps have ms precision
337337
)

tests/unit/test__pandas_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def test_list_columns_and_indexes_with_named_index_same_as_column_name(
886886

887887
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
888888
def test_dataframe_to_json_generator(module_under_test):
889-
utcnow = datetime.datetime.utcnow()
889+
utcnow = datetime.datetime.now(datetime.timezone.utc)
890890
dataframe = pandas.DataFrame(
891891
{
892892
"a_series": [1, 2, 3, 4],

tests/unit/test_client.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5853,7 +5853,7 @@ def test_insert_rows_w_schema(self):
58535853
from google.cloud.bigquery.schema import SchemaField
58545854

58555855
WHEN_TS = 1437767599.006
5856-
WHEN = datetime.datetime.utcfromtimestamp(WHEN_TS).replace(tzinfo=UTC)
5856+
WHEN = datetime.datetime.fromtimestamp(WHEN_TS, UTC).replace(tzinfo=UTC)
58575857
PATH = "projects/%s/datasets/%s/tables/%s/insertAll" % (
58585858
self.PROJECT,
58595859
self.DS_ID,
@@ -5914,7 +5914,7 @@ def test_insert_rows_w_list_of_dictionaries(self):
59145914
from google.cloud.bigquery.table import Table
59155915

59165916
WHEN_TS = 1437767599.006
5917-
WHEN = datetime.datetime.utcfromtimestamp(WHEN_TS).replace(tzinfo=UTC)
5917+
WHEN = datetime.datetime.fromtimestamp(WHEN_TS, UTC).replace(tzinfo=UTC)
59185918
PATH = "projects/%s/datasets/%s/tables/%s/insertAll" % (
59195919
self.PROJECT,
59205920
self.DS_ID,
@@ -6097,6 +6097,7 @@ def _row_data(row):
60976097
)
60986098

60996099
def test_insert_rows_w_repeated_fields(self):
6100+
from google.cloud._helpers import UTC
61006101
from google.cloud.bigquery.schema import SchemaField
61016102
from google.cloud.bigquery.table import Table
61026103

@@ -6126,12 +6127,8 @@ def test_insert_rows_w_repeated_fields(self):
61266127
(
61276128
12,
61286129
[
6129-
datetime.datetime(
6130-
2018, 12, 1, 12, 0, 0, tzinfo=datetime.timezone.utc
6131-
),
6132-
datetime.datetime(
6133-
2018, 12, 1, 13, 0, 0, tzinfo=datetime.timezone.utc
6134-
),
6130+
datetime.datetime(2018, 12, 1, 12, 0, 0, tzinfo=UTC),
6131+
datetime.datetime(2018, 12, 1, 13, 0, 0, tzinfo=UTC),
61356132
],
61366133
[1.25, 2.5],
61376134
),
@@ -6966,7 +6963,9 @@ def test_list_rows(self):
69666963
)
69676964
WHEN_TS = 1437767599006000
69686965

6969-
WHEN = datetime.datetime.utcfromtimestamp(WHEN_TS / 1e6).replace(tzinfo=UTC)
6966+
WHEN = datetime.datetime.fromtimestamp(
6967+
WHEN_TS / 1e6, datetime.timezone.utc
6968+
).replace(tzinfo=UTC)
69706969
WHEN_1 = WHEN + datetime.timedelta(microseconds=1)
69716970
WHEN_2 = WHEN + datetime.timedelta(microseconds=2)
69726971
ROWS = 1234

tests/unit/test_dataset.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,9 @@ def _setUpConstants(self):
945945
from google.cloud._helpers import UTC
946946

947947
self.WHEN_TS = 1437767599.006
948-
self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC)
948+
self.WHEN = datetime.datetime.fromtimestamp(self.WHEN_TS, UTC).replace(
949+
tzinfo=UTC
950+
)
949951
self.ETAG = "ETAG"
950952
self.DS_FULL_ID = "%s:%s" % (self.PROJECT, self.DS_ID)
951953
self.RESOURCE_URL = "http://example.com/path/to/resource"

tests/unit/test_query.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,9 @@ def test_to_api_repr_w_timestamp_datetime(self):
637637
self.assertEqual(param.to_api_repr(), EXPECTED)
638638

639639
def test_to_api_repr_w_timestamp_micros(self):
640-
from google.cloud._helpers import _microseconds_from_datetime
640+
from google.cloud._helpers import _microseconds_from_datetime, UTC
641641

642-
now = datetime.datetime.utcnow()
642+
now = datetime.datetime.now(UTC)
643643
seconds = _microseconds_from_datetime(now) / 1.0e6
644644
EXPECTED = {
645645
"parameterType": {"type": "TIMESTAMP"},
@@ -650,9 +650,9 @@ def test_to_api_repr_w_timestamp_micros(self):
650650
self.assertEqual(param.to_api_repr(), EXPECTED)
651651

652652
def test_to_api_repr_w_datetime_datetime(self):
653-
from google.cloud._helpers import _datetime_to_rfc3339
653+
from google.cloud._helpers import _datetime_to_rfc3339, UTC
654654

655-
now = datetime.datetime.utcnow()
655+
now = datetime.datetime.now(UTC)
656656
EXPECTED = {
657657
"parameterType": {"type": "DATETIME"},
658658
"parameterValue": {
@@ -664,9 +664,9 @@ def test_to_api_repr_w_datetime_datetime(self):
664664
self.assertEqual(param.to_api_repr(), EXPECTED)
665665

666666
def test_to_api_repr_w_datetime_string(self):
667-
from google.cloud._helpers import _datetime_to_rfc3339
667+
from google.cloud._helpers import _datetime_to_rfc3339, UTC
668668

669-
now = datetime.datetime.utcnow()
669+
now = datetime.datetime.now(UTC)
670670
now_str = _datetime_to_rfc3339(now)
671671
EXPECTED = {
672672
"parameterType": {"type": "DATETIME"},
@@ -1047,9 +1047,10 @@ def test_to_api_repr_w_datetime_str(self):
10471047
self.assertEqual(param.to_api_repr(), EXPECTED)
10481048

10491049
def test_to_api_repr_w_datetime_datetime(self):
1050+
from google.cloud._helpers import UTC # type: ignore
10501051
from google.cloud.bigquery._helpers import _RFC3339_MICROS_NO_ZULU
10511052

1052-
now = datetime.datetime.utcnow()
1053+
now = datetime.datetime.now(UTC)
10531054
now_str = now.strftime(_RFC3339_MICROS_NO_ZULU)
10541055
EXPECTED = {
10551056
"parameterType": {
@@ -1089,7 +1090,7 @@ def test_to_api_repr_w_timestamp_str(self):
10891090
def test_to_api_repr_w_timestamp_timestamp(self):
10901091
from google.cloud._helpers import UTC # type: ignore
10911092

1092-
now = datetime.datetime.utcnow()
1093+
now = datetime.datetime.now(UTC)
10931094
now = now.astimezone(UTC)
10941095
now_str = str(now)
10951096
EXPECTED = {

tests/unit/test_table.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ def _setUpConstants(self):
395395
from google.cloud._helpers import UTC
396396

397397
self.WHEN_TS = 1437767599.006
398-
self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC)
398+
self.WHEN = datetime.datetime.fromtimestamp(self.WHEN_TS, UTC).replace(
399+
tzinfo=UTC
400+
)
399401
self.ETAG = "ETAG"
400402
self.TABLE_FULL_ID = "%s:%s.%s" % (self.PROJECT, self.DS_ID, self.TABLE_NAME)
401403
self.RESOURCE_URL = "http://example.com/path/to/resource"
@@ -1952,7 +1954,9 @@ def _setUpConstants(self):
19521954
from google.cloud._helpers import UTC
19531955

19541956
self.WHEN_TS = 1437767599.125
1955-
self.WHEN = datetime.datetime.utcfromtimestamp(self.WHEN_TS).replace(tzinfo=UTC)
1957+
self.WHEN = datetime.datetime.fromtimestamp(self.WHEN_TS, UTC).replace(
1958+
tzinfo=UTC
1959+
)
19561960
self.EXP_TIME = datetime.datetime(2015, 8, 1, 23, 59, 59, tzinfo=UTC)
19571961

19581962
def test_ctor(self):

0 commit comments

Comments
 (0)