Enable ruff DTZ rules#1694
Conversation
datetime.now() without a tz argument returns a naive local-time value that silently drifts when serialized or compared against tz-aware datetimes. DTZ catches this class of bug at lint time. The single existing violation (aioesphomeapi/log_reader.py) switches to datetime.now().astimezone() so the timestamp is an aware local datetime — hour/minute/second values are unchanged, the user-visible log prefix stays identical.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1694 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 26 26
Lines 4165 4165
=========================================
Hits 4165 4165 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughThis PR updates the log timestamp generation to use timezone-aware datetime and enables the DTZ linting rule to enforce timezone-aware datetime practices across the codebase. The log prefix timestamp now includes timezone information by calling ChangesTimezone-aware datetime enforcement
🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Suggested reviewers
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Enables Ruff’s DTZ (flake8-datetimez) lint rule family to prevent introducing naive local datetimes, and updates the log reader timestamp generation accordingly.
Changes:
- Add
DTZto[tool.ruff.lint].selectinpyproject.toml. - Update
aioesphomeapi/log_reader.pyto use a timezone-aware timestamp for log prefix formatting.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pyproject.toml | Enables Ruff DTZ rules in the lint selection list. |
| aioesphomeapi/log_reader.py | Adjusts log timestamp creation to be tz-aware for DTZ compliance. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| def on_log(msg: SubscribeLogsResponse) -> None: | ||
| time_ = datetime.now() | ||
| time_ = datetime.now().astimezone() |
What does this implement/fix?
Enables the ruff
DTZ(flake8-datetimez) rule family.datetime.now()without atzargument returns a naive local-timevalue that silently drifts when serialized or compared against
tz-aware datetimes. DTZ catches this class of bug at lint time.
What changed
aioesphomeapi/log_reader.py:64switchesdatetime.now()todatetime.now().astimezone(). The result is an aware localdatetime —
.hour,.minute,.second,.microsecondvaluesare unchanged, so the user-visible log prefix
(
[HH:MM:SS.mmm]) stays identical. Only DTZ violation in therepo.
pyproject.tomladds"DTZ"to the[tool.ruff.lint] selectlist (alphabetical position, between
BandE).Follows the established cadence of one ruff family per PR
(PERF, PTH #1678, PT006/PT007 #1679, B #1683, PGH #1684).
Types of changes
Related issue or feature (if applicable):
Pull request in esphome (if applicable):
Checklist:
tests/folder). (Existingtests/test_log_reader.pycovers the timestamp callback path; no behavior change.)Quality Report
Changes: 2 files changed, 2 insertions(+), 1 deletion(-)
Code scan: clean
Tests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline