fix(reports): stamp email subject date at send time, not import time#40693
fix(reports): stamp email subject date at send time, not import time#40693rusackas wants to merge 1 commit into
Conversation
`EmailNotification.now` was a class attribute evaluated once when the module is first imported. In a long-running worker that means every report email with DATE_FORMAT_IN_EMAIL_SUBJECT enabled renders the date the process started rather than the actual send date. It also made test_email_subject_with_datetime flaky: the test sampled its own clock separately from the frozen class attribute, so a run crossing the UTC midnight boundary compared two different dates and failed (e.g. `assert '2026-06-03' in '... 2026-06-02'`). Move the timestamp to a per-instance value set in __init__ so each notification reflects its real creation/send time, and assert against the notification's own `now` in the test so it can't flake at the day boundary. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Code Review Agent Run #f66398Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #40693 +/- ##
=======================================
Coverage 64.05% 64.05%
=======================================
Files 2662 2662
Lines 143254 143256 +2
Branches 32941 32941
=======================================
+ Hits 91764 91766 +2
Misses 49903 49903
Partials 1587 1587
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR fixes report email subjects being stamped with a stale date by moving the “now” timestamp from an import-time class attribute to an instance attribute set at notification creation time, and updates the corresponding unit test to avoid midnight-boundary flakiness.
Changes:
- Stamp
EmailNotification.nowper instance in__init__(instead of once at module import) so subject date formatting reflects the actual notification creation/send time. - Update
test_email_subject_with_datetimeto assert againstnotification.now(the same timestamp used to render the subject), eliminating UTC-midnight flakes. - Remove now-unused
datetime/timezoneimports from the test module.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
superset/reports/notifications/email.py |
Move now stamping to EmailNotification.__init__ so date formatting is per-notification rather than frozen at worker import time. |
tests/unit_tests/reports/notifications/email_tests.py |
Make the datetime subject assertion use the notification’s own stamped timestamp to prevent midnight-boundary test flakiness. |
| # Compare against the notification's own stamped timestamp rather than a | ||
| # separately-sampled clock, so the assertion can't flake when the test runs | ||
| # across the UTC midnight boundary. | ||
| assert notification.now.strftime(datetime_pattern) in subject |
There was a problem hiding this comment.
Both sides of this assertion read notification.now, so if someone reverted this fix, this test would still pass. We should update the test or add one such that any revert to your fix would fail the test.
|
The current test assertion tests/unit_tests/reports/notifications/email_tests.py |
SUMMARY
EmailNotification.nowwas a class attribute evaluated once, when the module is first imported:Two problems flow from this:
Latent product bug: in a long-running worker, every report email rendered with
DATE_FORMAT_IN_EMAIL_SUBJECTenabled stamps the subject with the date the process started, not the actual send date. A worker up for several days would put a stale date in every alert/report subject.Flaky CI:
test_email_subject_with_datetimereads two different clocks — the class attribute (frozen at import) and a freshdatetime.now()it samples itself — and asserts they render the same%Y-%m-%d. Those two samples are taken minutes apart, so a unit-test job straddling UTC midnight breaks the assumption:23:54:xxemail.py, soEmailNotification.nowis evaluated → frozen2026-06-0200:07:xxnow, builds the subject (which uses the frozen class attribute), and asserts2026-06-03The subject carries the import-time date while the test compares against the run-time date, so once the run crosses midnight they disagree:
Any unit-test job whose run happens to span 00:00 UTC trips this — which is why it was failing on several unrelated open PRs (chore(mcp): return a generic error from the webdriver pool-stats endpoint #40559, fix(embedded): enforce configured allowed domains for postMessage origin #40629, fix(export): sanitize user-supplied CSV export filename (charts + SQL Lab) #40632, fix(jinja): apply consistent escaping to url_param values from request args #40633) at the same time. Same single flake, not separate bugs. (It also self-heals on a re-run that doesn't cross midnight, which is what made it look intermittent.)
Changes
superset/reports/notifications/email.py: stampself.nowonce per instance in__init__(at send time) instead of once per process at import. Each notification now reflects its real creation/send time.tests/unit_tests/reports/notifications/email_tests.py: assert against the notification's ownnowrather than a separately-sampled clock, so the test can't flake at the day boundary. Drops the now-unuseddatetime/timezoneimports.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A
TESTING INSTRUCTIONS
3/3 pass. The fixed test no longer depends on wall-clock alignment across the midnight boundary.
ADDITIONAL INFORMATION
🤖 Generated with Claude Code