Skip to content

fix(models): make naive datetime object timezone-aware before converting to unix timestamp - #39782

Merged
rusackas merged 4 commits into
apache:masterfrom
hy144328:test/test_dttm_sql_literal
Jun 23, 2026
Merged

rusackas merged 4 commits into
apache:masterfrom
hy144328:test/test_dttm_sql_literal

Conversation

@hy144328

@hy144328 hy144328 commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #39781 .

SUMMARY

Currently, two testcases rely on the environment to have UTC timezone in order to pass.
The problem is that given timestamp is timezone-native, and that the solution timestamp is a UNIX timestamp, and thus timezone-aware.
Fix the timezone for those timestamps.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

This test passes here but not on the main branch:

env TZ='US/Eastern' python3 -m pytest tests/unit_tests/models/core_test.py -k test_dttm_sql_literal

ADDITIONAL INFORMATION

  • Has associated issue: test_dttm_sql_literal() fails in non-UTC environment #39781
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@bito-code-review

bito-code-review Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #0014a6

Actionable Suggestions - 0
Additional Suggestions - 1
  • tests/unit_tests/models/core_test.py - 1
    • Test Data Inconsistency · Line 204-210
      The diff updates some datetime objects to be timezone-aware, but others in the same test remain naive. This creates inconsistency in test data creation. Since dttm_sql_literal uses timestamp() for epoch formats, which assumes local time for naive datetimes, using timezone-aware objects ensures predictable behavior across environments.
Review Details
  • Files reviewed - 1 · Commit Range: c097733..c097733
    • tests/unit_tests/models/core_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@dosubot dosubot Bot added the global:timezone Related to timezones label Apr 30, 2026
@netlify

netlify Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 14ea0eb
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a0606b3f2de610008f1ce57
😎 Deploy Preview https://deploy-preview-39782--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@hy144328
hy144328 force-pushed the test/test_dttm_sql_literal branch from c097733 to 6e2720b Compare April 30, 2026 13:58
@codecov

codecov Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.36%. Comparing base (acb996a) to head (9093f93).

Files with missing lines Patch % Lines
superset/models/helpers.py 20.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #39782      +/-   ##
==========================================
- Coverage   64.36%   64.36%   -0.01%     
==========================================
  Files        2653     2653              
  Lines      145118   145121       +3     
  Branches    33473    33474       +1     
==========================================
- Hits        93411    93406       -5     
- Misses      50016    50022       +6     
- Partials     1691     1693       +2     
Flag Coverage Δ
hive 39.25% <20.00%> (-0.01%) ⬇️
mysql 57.98% <20.00%> (-0.01%) ⬇️
postgres 58.04% <20.00%> (-0.01%) ⬇️
presto 40.83% <20.00%> (-0.01%) ⬇️
python 59.48% <20.00%> (-0.01%) ⬇️
sqlite 57.70% <20.00%> (-0.01%) ⬇️
unit 100.00% <ø> (ø)

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hy144328
hy144328 force-pushed the test/test_dttm_sql_literal branch 2 times, most recently from 801d31c to 992e9b0 Compare April 30, 2026 14:57
@hy144328 hy144328 changed the title test: fix test by comparing timezone-specific timestamps test: fix test by comparing timezone-aware timestamps Apr 30, 2026
@hy144328
hy144328 force-pushed the test/test_dttm_sql_literal branch from 992e9b0 to ec790df Compare April 30, 2026 18:22
@bito-code-review

bito-code-review Bot commented May 1, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #8e8ef1

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: ec790df..ec790df
    • tests/unit_tests/models/core_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@pull-request-size pull-request-size Bot added size/S and removed size/XS labels May 4, 2026
@hy144328

hy144328 commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

@Mayankaggarwal8055 Thanks for the feedback, and sorry for the late reply!

Also might be worth adding a quick comment in the test about why this is needed, just so it’s clearer for anyone reading it later.

Good point.
I have just added a comment to the parameterization of the testcase.
Hope that it makes things clearer.

Just wondering — are both timestamps being compared in the same timezone explicitly, or does it rely on how Python handles it internally?

It relies on how Python implicitly handles timezone-naive timestamps.
If no timezone is provided, the timestamp is assumed relative to the local timezone.
If input and output timestamps are both timezone-naive, this is acceptable.
If exactly one timestamp is timezone-naive, this will inevitably lead to failure unless the timezone is UTC.

@bito-code-review

bito-code-review Bot commented May 4, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #24174

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: ec790df..a56bf63
    • tests/unit_tests/models/core_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@rusackas

rusackas commented May 12, 2026 •

Copy link
Copy Markdown
Member

Running CI 🤞

Noting that #40034 tackles this implementation as well... taking a look at that too.

@rusackas

Copy link
Copy Markdown
Member

This fix makes the test environment-independent, but it does so by changing the nature of the test input — the datetimes are no longer naive. The production code path being tested (dttm.timestamp() on a naive datetime) is what actually runs in the real application, because the callers throughout the Superset codebase pass naive datetimes. So the test is now no longer testing the real code path — it's testing a scenario that doesn't happen in practice (a timezone-aware input), and the underlying bug in the implementation (where a naive datetime produces a timezone-dependent epoch) remains live and unfixed.

The best aproach would be to combine elements of both...

  1. Fix the implementation to handle naive datetimes deterministically (using timezone.utc, not datetime.UTC), with a proper astimezone() guard for already-aware datetimes.
  2. Keep the test inputs naive (as they reflect real usage), so the test continues to cover the actual production code path.

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

Note

Copilot was unable to run its full agentic suite in this review.

Fixes flaky unit tests that depended on the host environment’s local timezone by making the test datetimes explicitly timezone-aware (UTC) so they compare correctly against epoch-based expected values.

Changes:

  • Import timezone and use timezone.utc on test datetimes used for epoch formatting assertions
  • Add clarifying comments explaining timezone-naive vs timezone-aware expectations in the parametrized test data

Comment thread tests/unit_tests/models/core_test.py Resolved
Comment thread tests/unit_tests/models/core_test.py Resolved
@hy144328

Copy link
Copy Markdown
Contributor Author

This fix makes the test environment-independent, but it does so by changing the nature of the test input — the datetimes are no longer naive. The production code path being tested (dttm.timestamp() on a naive datetime) is what actually runs in the real application, because the callers throughout the Superset codebase pass naive datetimes. So the test is now no longer testing the real code path — it's testing a scenario that doesn't happen in practice (a timezone-aware input), and the underlying bug in the implementation (where a naive datetime produces a timezone-dependent epoch) remains live and unfixed.

The best aproach would be to combine elements of both...

1. Fix the implementation to handle naive datetimes deterministically (using timezone.utc, not datetime.UTC), with a proper astimezone() guard for already-aware datetimes.

2. Keep the test inputs naive (as they reflect real usage), so the test continues to cover the actual production code path.

@rusackas Thank you for the clarification.
I was reluctant to change the code because, as the one who originally raised #39781 , I observed this bug when running tests locally but not in production.
This said, most servers probably run on UTC so this bug will stay unnoticed for a long time.

As suggested, I have reverted my own changes to the unit test, and applied @charliesheh 's ideas from #40034 .
Would you please give this PR another review?
Apologies for the extra loop.
This would be my first contribution to this project.
Happy to squash into a single commit if the history is not needed anymore.

@hy144328
hy144328 force-pushed the test/test_dttm_sql_literal branch from bac86f2 to 9dcf023 Compare May 14, 2026 18:58
@hy144328

Copy link
Copy Markdown
Contributor Author

/review

@hy144328
hy144328 force-pushed the test/test_dttm_sql_literal branch from 9dcf023 to 9eb7e03 Compare May 14, 2026 19:12

@bito-code-review bito-code-review Bot 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.

Code Review Agent Run #354d9b

Actionable Suggestions - 1
  • superset/models/helpers.py - 1
Review Details
  • Files reviewed - 1 · Commit Range: 52fa7f3..9dcf023
    • superset/models/helpers.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

Comment thread superset/models/helpers.py Resolved
@hy144328

Copy link
Copy Markdown
Contributor Author

/review

@bito-code-review

bito-code-review Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #ad8868

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 52fa7f3..9eb7e03
    • superset/models/helpers.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@bito-code-review

bito-code-review Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #319287

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 52fa7f3..9eb7e03
    • superset/models/helpers.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@rusackas rusackas left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for circling back on this, and for your patience. The if tzinfo is None guard with timezone.utc LGTM, and avoids the clobbering-aware-datetimes issue that the unconditional replace() in #40034 would hit. I'm good with this approach. I'm considering closing #40034 in favor of this one so we're not landing the same fix twice — feel free to rebase when possible (there's currently a conflict), and we can take that route. 🤞

@hy144328
hy144328 force-pushed the test/test_dttm_sql_literal branch from 9eb7e03 to 8cd54d9 Compare June 15, 2026 21:55
@hy144328

Copy link
Copy Markdown
Contributor Author

Thanks for circling back on this, and for your patience. The if tzinfo is None guard with timezone.utc LGTM, and avoids the clobbering-aware-datetimes issue that the unconditional replace() in #40034 would hit. I'm good with this approach. I'm considering closing #40034 in favor of this one so we're not landing the same fix twice — feel free to rebase when possible (there's currently a conflict), and we can take that route. 🤞

@rusackas As suggested, I rebased in order to resolve the merge conflicts.
Turned out to only be some noise at the import statements.
I force-pushed the rebased branch, and double-checked that the testcase in question still passes (see description):

env TZ='US/Eastern' python3 -m pytest tests/unit_tests/models/core_test.py -k test_dttm_sql_literal

Ready whenever you are! :-)

@bito-code-review

bito-code-review Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #90dae2

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: d0e8d62..8cd54d9
    • superset/models/helpers.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@rusackas rusackas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving CI, for starters.

This seems to be landing the right fix: dttm.timestamp() interprets naive datetimes as local time, so epoch_s/epoch_ms output drifted with the server's TZ. Forcing UTC before timestamp() in dttm_sql_literal makes it deterministic, and replace() keeps it non-mutating.

Approving, but the only nit is the title still says test: when this is really a fix(models): change. Happy to change the title if you'd like me to.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Translation Regression Detected

A source change in this PR renamed or reworded strings, invalidating existing translations (they are now #, fuzzy) in de, fi, it, ko, th. Please resolve the affected .po files before merging.

Note: intentionally deleting a translatable string is not a regression and is not flagged here — only translations invalidated by a renamed/reworded source string are.

Language Fuzzy before Fuzzy after New
de 0 997 +997
fi 4815 4823 +8
it 1665 1667 +2
ko 1519 1521 +2
th 4815 4823 +8

How to fix

1. Install dependencies (if not already set up):

pip install -r superset/translations/requirements.txt
sudo apt-get install gettext   # or: brew install gettext

2. Re-extract strings and sync .po files:

./scripts/translations/babel_update.sh

This rewrites superset/translations/messages.pot from the current source files and merges the changes into every .po file. Strings whose msgid changed will be marked #, fuzzy.

3. Resolve the fuzzy entries in the affected language files (de, fi, it, ko, th):

grep -n '#, fuzzy' superset/translations/<lang>/LC_MESSAGES/messages.po

For each fuzzy entry, either rewrite the msgstr to match the new string and remove the #, fuzzy line, or clear the msgstr to "" if you cannot provide a translation.

4. Commit your changes to the .po files.

@hy144328 hy144328 changed the title test: fix test by comparing timezone-aware timestamps fix(models): make naive datetime object timezone-aware before converting to unix timestamp Jun 23, 2026
@hy144328
hy144328 force-pushed the test/test_dttm_sql_literal branch from 8cd54d9 to 9093f93 Compare June 23, 2026 19:35
@hy144328

Copy link
Copy Markdown
Contributor Author

Approving CI, for starters.

This seems to be landing the right fix: dttm.timestamp() interprets naive datetimes as local time, so epoch_s/epoch_ms output drifted with the server's TZ. Forcing UTC before timestamp() in dttm_sql_literal makes it deterministic, and replace() keeps it non-mutating.

Approving, but the only nit is the title still says test: when this is really a fix(models): change. Happy to change the title if you'd like me to.

@rusackas Thanks, I updated the title!

By the way, GitHub Actions sent a message regarding translations. #39782 (comment)
But it disappeared after rebasing. 👌

@rusackas
rusackas merged commit 9b6d3ce into apache:master Jun 23, 2026
59 checks passed
@rusackas

Copy link
Copy Markdown
Member

Thanks again!

@bito-code-review

Copy link
Copy Markdown
Contributor

Bito Automatic Review Skipped – PR Already Merged

Bito scheduled an automatic review for this pull request, but the review was skipped because this PR was merged before the review could be run.
No action is needed if you didn't intend to review it. To get a review, you can type /review in a comment and save it

shantanukhond pushed a commit to shantanukhond/superset that referenced this pull request Jun 24, 2026
rusackas added a commit to jayakarkatika/superset that referenced this pull request Aug 4, 2026
…xtra values

Rebases the DST-aware timezone conversion onto master, which since merged
apache#41391's fixed Hour Offset shift for the same get_time_filter method. The
two mechanisms now layer instead of conflict: a configured extra.timezone
takes precedence (it is strictly more correct, since it handles DST), and
the legacy Hour Offset still applies exactly as before when no timezone is
configured.

Also addresses two outstanding review threads on get_time_filter:
- get_dataset_timezone now validates that extra["timezone"] is a string
  before returning it, since extra is arbitrary user-supplied JSON and a
  non-string value would otherwise reach ZoneInfo() and raise TypeError.
  get_time_filter also catches TypeError alongside ZoneInfoNotFoundError
  as defense in depth.
- Confirmed the epoch-rendering concern (naive datetime .timestamp() being
  interpreted in the server's local timezone) is already fixed on master
  via apache#39782's UTC-anchoring in dttm_sql_literal; added a regression test
  tying it to the dataset-timezone path specifically.

Adds test coverage for the reconciliation (offset-only fallback, timezone
taking precedence over a configured offset) and the non-string timezone
guard.

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

global:timezone Related to timezones size/S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_dttm_sql_literal() fails in non-UTC environment

3 participants