[core] Enable ruff DTZ (flake8-datetimez) lint family (#16660)

This commit is contained in:
J. Nick Koston
2026-05-26 15:29:38 -05:00
committed by GitHub
parent bac62cb7de
commit 96816e2491
12 changed files with 40 additions and 28 deletions

View File

@@ -120,7 +120,7 @@ def test_is_file_recent_with_old_file(setup_core: Path) -> None:
old_time = time.time() - 7200
mock_stat = MagicMock()
mock_stat.st_ctime = old_time
mock_stat.st_mtime = old_time
with patch.object(Path, "stat", return_value=mock_stat):
refresh = TimePeriod(seconds=3600)
@@ -147,7 +147,7 @@ def test_is_file_recent_with_zero_refresh(setup_core: Path) -> None:
# Mock stat to return a time 10 seconds ago
mock_stat = MagicMock()
mock_stat.st_ctime = time.time() - 10
mock_stat.st_mtime = time.time() - 10
with patch.object(Path, "stat", return_value=mock_stat):
refresh = TimePeriod(seconds=0)
result = external_files.is_file_recent(test_file, refresh)

View File

@@ -1,8 +1,8 @@
"""Tests for git.py module."""
from datetime import datetime, timedelta
import os
from pathlib import Path
import time
from typing import Any
from unittest.mock import Mock, patch
@@ -34,9 +34,9 @@ def _setup_old_repo(repo_dir: Path, days_old: int = 2) -> None:
# Create FETCH_HEAD file with old timestamp
fetch_head = git_dir / "FETCH_HEAD"
fetch_head.write_text("test")
old_time = datetime.now() - timedelta(days=days_old)
old_time = time.time() - days_old * 86400
fetch_head.touch()
os.utime(fetch_head, (old_time.timestamp(), old_time.timestamp()))
os.utime(fetch_head, (old_time, old_time))
def _get_git_command_type(cmd: list[str]) -> str | None:
@@ -285,10 +285,10 @@ def test_clone_or_update_with_refresh_updates_old_repo(
# Create FETCH_HEAD file with old timestamp (2 days ago)
fetch_head = git_dir / "FETCH_HEAD"
fetch_head.write_text("test")
old_time = datetime.now() - timedelta(days=2)
old_time = time.time() - 2 * 86400
fetch_head.touch() # Create the file
# Set modification time to 2 days ago
os.utime(fetch_head, (old_time.timestamp(), old_time.timestamp()))
os.utime(fetch_head, (old_time, old_time))
# Mock git command responses
mock_run_git_command.return_value = "abc123" # SHA for rev-parse
@@ -333,10 +333,10 @@ def test_clone_or_update_with_refresh_skips_fresh_repo(
# Create FETCH_HEAD file with recent timestamp (1 hour ago)
fetch_head = git_dir / "FETCH_HEAD"
fetch_head.write_text("test")
recent_time = datetime.now() - timedelta(hours=1)
recent_time = time.time() - 3600
fetch_head.touch() # Create the file
# Set modification time to 1 hour ago
os.utime(fetch_head, (recent_time.timestamp(), recent_time.timestamp()))
os.utime(fetch_head, (recent_time, recent_time))
# Call with refresh=1d (1 day)
refresh = TimePeriodSeconds(days=1)
@@ -409,10 +409,10 @@ def test_clone_or_update_with_none_refresh_always_updates(
# Create FETCH_HEAD file with very recent timestamp (1 second ago)
fetch_head = git_dir / "FETCH_HEAD"
fetch_head.write_text("test")
recent_time = datetime.now() - timedelta(seconds=1)
recent_time = time.time() - 1
fetch_head.touch() # Create the file
# Set modification time to 1 second ago
os.utime(fetch_head, (recent_time.timestamp(), recent_time.timestamp()))
os.utime(fetch_head, (recent_time, recent_time))
# Mock git command responses
mock_run_git_command.return_value = "abc123" # SHA for rev-parse

View File

@@ -576,8 +576,8 @@ def test_esphome_storage_json_last_update_check_property() -> None:
assert result.hour == 10
assert result.minute == 30
# Test setter
new_date = datetime(2024, 2, 20, 15, 45, 30)
# Test setter — naive datetime matches the storage round-trip format.
new_date = datetime(2024, 2, 20, 15, 45, 30) # noqa: DTZ001
storage.last_update_check = new_date
assert storage.last_update_check_str == "2024-02-20T15:45:30"