Skip to content

Enable ruff DTZ rules#1694

Merged
bdraco merged 1 commit into
esphome:mainfrom
bluetoothbot:koan/enable-ruff-dtz
May 20, 2026
Merged

Enable ruff DTZ rules#1694
bdraco merged 1 commit into
esphome:mainfrom
bluetoothbot:koan/enable-ruff-dtz

Conversation

@bluetoothbot

@bluetoothbot bluetoothbot commented May 20, 2026

Copy link
Copy Markdown
Contributor

What does this implement/fix?

Enables the ruff DTZ (flake8-datetimez) rule family.

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.

What changed

  • aioesphomeapi/log_reader.py:64 switches datetime.now() to
    datetime.now().astimezone(). The result is an aware local
    datetime — .hour, .minute, .second, .microsecond values
    are unchanged, so the user-visible log prefix
    ([HH:MM:SS.mmm]) stays identical. Only DTZ violation in the
    repo.
  • pyproject.toml adds "DTZ" to the [tool.ruff.lint] select
    list (alphabetical position, between B and E).

Follows the established cadence of one ruff family per PR
(PERF, PTH #1678, PT006/PT007 #1679, B #1683, PGH #1684).

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

  • n/a

Pull request in esphome (if applicable):

  • n/a

Checklist:

  • The code change is tested and works locally.
  • If api.proto was modified, a linked pull request has been made to esphome with the same changes. (api.proto not modified.)
  • Tests have been added to verify that the new code works (under tests/ folder). (Existing tests/test_log_reader.py covers 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

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

codecov Bot commented May 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (dcd9453) to head (f6f9a2b).
⚠️ Report is 2 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq

codspeed-hq Bot commented May 20, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 47 untouched benchmarks


Comparing bluetoothbot:koan/enable-ruff-dtz (f6f9a2b) with main (dcd9453)

Open in CodSpeed

@bdraco bdraco marked this pull request as ready for review May 20, 2026 22:59
Copilot AI review requested due to automatic review settings May 20, 2026 22:59
@bdraco bdraco merged commit 637c616 into esphome:main May 20, 2026
15 of 16 checks passed
@coderabbitai

coderabbitai Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b9522ee2-469d-4bf1-ac78-f56c76c54bdd

📥 Commits

Reviewing files that changed from the base of the PR and between dcd9453 and f6f9a2b.

📒 Files selected for processing (2)
  • aioesphomeapi/log_reader.py
  • pyproject.toml

Walkthrough

This 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 datetime.now().astimezone().

Changes

Timezone-aware datetime enforcement

Layer / File(s) Summary
Timezone-aware datetime and DTZ linting enforcement
aioesphomeapi/log_reader.py, pyproject.toml
Log timestamp generation is updated from naive datetime.now() to timezone-aware datetime.now().astimezone(). The Ruff DTZ rule group (flake8-datetimez) is enabled to enforce timezone-aware datetime usage across the project.

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • esphome/aioesphomeapi#1683: Both PRs modify Ruff lint configuration in pyproject.toml, adding different rule groups to enforce code standards.
  • esphome/aioesphomeapi#915: Both PRs update the Ruff lint configuration in pyproject.toml to add new rule sets for code quality enforcement.

Suggested reviewers

  • bdraco
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DTZ to [tool.ruff.lint].select in pyproject.toml.
  • Update aioesphomeapi/log_reader.py to 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()
@bluetoothbot bluetoothbot deleted the koan/enable-ruff-dtz branch May 21, 2026 00:36
@github-actions github-actions Bot locked and limited conversation to collaborators May 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants