Skip to content

Fix: 支持带时区的 expire_time 过期判断 - #4237

Merged
yanyongyu merged 5 commits into
nonebot:masterfrom
mayuriphad:fix/aware-expire-time
Nov 4, 2026
Merged

yanyongyu merged 5 commits into
nonebot:masterfrom
mayuriphad:fix/aware-expire-time

Conversation

@mayuriphad

Copy link
Copy Markdown
Contributor

Fixes #4236

_check_matcher compared a naive datetime.now() against Matcher.expire_time. When expire_time is a timezone-aware datetime (allowed by the datetime | timedelta annotation, e.g. datetime.now(timezone.utc) + timedelta(minutes=5)), the comparison raises TypeError: can't compare offset-naive and offset-aware datetimes outside the permission/rule try blocks. The matcher is then never destroyed and keeps failing on every event.

This takes "now" in the expire time's own timezone: datetime.now(Matcher.expire_time.tzinfo). For naive expire_time (including the timedelta path, which produces a naive datetime) tzinfo is None, so behaviour is unchanged.

Added test_aware_datetime_expire, mirroring test_datetime_expire with an already expired UTC-aware expire_time, and checked that the matcher gets destroyed.

ruff format --check and ruff check pass on the changed files. I have not run the pytest suite locally; relying on CI for it.

_check_matcher compared naive datetime.now() with expire_time, raising
TypeError for timezone-aware datetimes and leaving the matcher alive.
Take now() in expire_time's own tzinfo instead.

Fixes #4236

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 29, 2026 05:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
@yanyongyu yanyongyu added the bug Something isn't working label Nov 4, 2026
@yanyongyu
yanyongyu requested a balanced review from Copilot November 4, 2026 11:34
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to https://deploy-preview-4237--nonebot2.netlify.app

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.85%. Comparing base (8ea99f1) to head (37ffbc5).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4237      +/-   ##
==========================================
+ Coverage   93.84%   93.85%   +0.01%     
==========================================
  Files          49       49              
  Lines        4532     4540       +8     
==========================================
+ Hits         4253     4261       +8     
  Misses        279      279              
Flag Coverage Δ
unittests 93.85% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The expiry comparison can prematurely destroy matchers during a DST rollback because datetimes sharing a tzinfo object are compared by wall-clock fields rather than actual instants.

1 open issue

Review effort: Balanced

Comment thread nonebot/message.py Resolved
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
yanyongyu
yanyongyu previously approved these changes Nov 4, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@yanyongyu
yanyongyu merged commit aa01ef3 into nonebot:master Nov 4, 2026
25 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Development

Successfully merging this pull request may close these issues.

Bug: Matcher.expire_time 传入带时区的 datetime 时,_check_matcher 每次检查抛 TypeError 且响应器永不销毁

3 participants