Skip to content

Commit 90bbdea

Browse files
committed
refactor: use datetime.UTC instead of timezone.utc
Fixes ruff errors. Replace all instances of `datetime.timezone.utc` with `datetime.UTC` for consistency and to leverage the new standard introduced in Python 3.11. Update imports accordingly. Also remove unnecessary noqa comments for PERF203 and modernize some type checks. No functional changes intended.
1 parent 89a5973 commit 90bbdea

File tree

18 files changed

+28
-36
lines changed

18 files changed

+28
-36
lines changed

cardano_node_tests/chang_us_coverage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def main() -> int:
6868
url = req_data.get("url")
6969
report = report.replace(f"https://github.com/{req_id}-404", url)
7070

71-
report = report.replace("TODAY", str(datetime.datetime.now(tz=datetime.timezone.utc).date()))
71+
report = report.replace("TODAY", str(datetime.datetime.now(tz=datetime.UTC).date()))
7272

7373
with open(args.output_report, "w", encoding="utf-8") as out_fp:
7474
out_fp.write(report)

cardano_node_tests/cluster_management/manager.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ def log(self, msg: str) -> None:
113113
locking.FileLockIfXdist(self.log_lock),
114114
open(configuration.SCHEDULING_LOG, "a", encoding="utf-8") as logfile,
115115
):
116-
logfile.write(
117-
f"{datetime.datetime.now(tz=datetime.timezone.utc)} on {self.worker_id}: {msg}\n"
118-
)
116+
logfile.write(f"{datetime.datetime.now(tz=datetime.UTC)} on {self.worker_id}: {msg}\n")
119117

120118
def save_worker_cli_coverage(self) -> None:
121119
"""Save CLI coverage info collected by this pytest worker.

cardano_node_tests/tests/kes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def check_kes_period_info_result( # noqa: C901
6161
errors.append(f"The kes expiration date is `null` in check '{check_id}' -> issue #4396?")
6262
elif command_metrics["qKesKesKeyExpiry"]:
6363
expected_expiration_date = (
64-
datetime.datetime.now(tz=datetime.timezone.utc)
64+
datetime.datetime.now(tz=datetime.UTC)
6565
+ datetime.timedelta(
6666
seconds=command_metrics["qKesRemainingSlotsInKesPeriod"] * cluster_obj.slot_length
6767
)

cardano_node_tests/tests/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ def test_slot_number(
14001400
"""Test `query slot-number`."""
14011401
common.get_test_id(cluster)
14021402

1403-
timestamp = datetime.datetime.now(datetime.timezone.utc)
1403+
timestamp = datetime.datetime.now(datetime.UTC)
14041404
slot_number = cluster.g_query.get_slot_number(timestamp=timestamp)
14051405

14061406
# In case the test runs on epoch boundary, the tip could still be in the previous epoch.
@@ -1423,7 +1423,7 @@ def test_slot_number_invalid_format(
14231423
"""
14241424
common.get_test_id(cluster)
14251425

1426-
timestamp_str = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d")
1426+
timestamp_str = datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%d")
14271427

14281428
with pytest.raises(clusterlib.CLIError) as excinfo:
14291429
cluster.g_query.query_cli(["slot-number", timestamp_str])
@@ -1445,7 +1445,7 @@ def test_slot_number_out_of_range(
14451445
"""
14461446
common.get_test_id(cluster)
14471447

1448-
now = datetime.datetime.now(datetime.timezone.utc)
1448+
now = datetime.datetime.now(datetime.UTC)
14491449

14501450
timestamp = now.replace(year=now.year * 4) if time_val == "above" else now.replace(year=1)
14511451

cardano_node_tests/tests/test_dbsync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import logging
44
import time
55
import typing as tp
6+
from datetime import UTC
67
from datetime import datetime
78
from datetime import timedelta
8-
from datetime import timezone
99

1010
import allure
1111
import pytest
@@ -417,7 +417,7 @@ def test_latest_snapshot_freshness(
417417
LOGGER.info(f"Snapshot size: {latest_snapshot.size_gb:.2f} GB")
418418

419419
# 3. Perform freshness check
420-
now_utc = datetime.now(timezone.utc)
420+
now_utc = datetime.now(UTC)
421421
five_days_ago = now_utc - timedelta(days=5)
422422

423423
assert latest_snapshot.last_modified >= five_days_ago, (

cardano_node_tests/tests/test_kes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,12 @@ def _check_kes_period_info(
296296

297297
with logfiles.expect_errors(expected_errors, worker_id=worker_id):
298298
LOGGER.info(
299-
f"{datetime.datetime.now(tz=datetime.timezone.utc)}: "
299+
f"{datetime.datetime.now(tz=datetime.UTC)}: "
300300
f"Waiting for slot no {expire_slot} for KES expiration."
301301
)
302302
cluster.wait_for_slot(slot=expire_slot)
303303
LOGGER.info(
304-
f"{datetime.datetime.now(tz=datetime.timezone.utc)}: "
304+
f"{datetime.datetime.now(tz=datetime.UTC)}: "
305305
f"KES expired (?); tip: '{cluster.g_query.get_tip()}'."
306306
)
307307

@@ -339,7 +339,7 @@ def _check_kes_period_info(
339339
)
340340

341341
LOGGER.info(
342-
f"{datetime.datetime.now(tz=datetime.timezone.utc)}: "
342+
f"{datetime.datetime.now(tz=datetime.UTC)}: "
343343
"Waiting 90 secs to make sure the expected errors make it to log files."
344344
)
345345
time.sleep(90)

cardano_node_tests/tests/test_reconnect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def _assert() -> None:
284284
for check_no in range(1, 11):
285285
try:
286286
_assert()
287-
except AssertionError: # noqa: PERF203
287+
except AssertionError:
288288
if check_no == 10:
289289
raise
290290
LOGGER.info(f"AssertionError on check {check_no}")

cardano_node_tests/tests/test_tx_mempool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_query_mempool_txin(
8888
for r in range(5):
8989
try:
9090
cluster.g_transaction.submit_tx_bare(tx_file=out_file_signed)
91-
except clusterlib.CLIError as exc: # noqa: PERF203
91+
except clusterlib.CLIError as exc:
9292
exc_str = str(exc)
9393
inputs_spent = (
9494
"All inputs are spent" in exc_str # In cardano-node >= 10.6.0

cardano_node_tests/tests/tests_plutus/test_mint_build.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ def test_time_range_minting(
412412

413413
# POSIX timestamp + offset
414414
redeemer_value = (
415-
int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp() * 1_000)
416-
+ timestamp_offset_ms
415+
int(datetime.datetime.now(tz=datetime.UTC).timestamp() * 1_000) + timestamp_offset_ms
417416
)
418417

419418
policyid = cluster.g_transaction.get_policyid(plutus_v_record.script_file)
@@ -661,8 +660,7 @@ def test_two_scripts_minting(
661660

662661
# POSIX timestamp + offset
663662
redeemer_value_timerange = (
664-
int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp() * 1_000)
665-
+ timestamp_offset_ms
663+
int(datetime.datetime.now(tz=datetime.UTC).timestamp() * 1_000) + timestamp_offset_ms
666664
)
667665

668666
policyid2 = cluster.g_transaction.get_policyid(script_file2)

cardano_node_tests/tests/tests_plutus/test_mint_negative_build.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,7 @@ def test_time_range_missing_tx_validity(
420420

421421
# POSIX timestamp + offset
422422
redeemer_value = (
423-
int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp() * 1_000)
424-
+ timestamp_offset_ms
423+
int(datetime.datetime.now(tz=datetime.UTC).timestamp() * 1_000) + timestamp_offset_ms
425424
)
426425

427426
policyid = cluster.g_transaction.get_policyid(plutus_v_record.script_file)

0 commit comments

Comments
 (0)