Repository navigation
fix(common/time_utils): correct None/empty timestamp fallback and ISO 8601 parsing - #16173
Harsh23Kashyap wants to merge 0 commit into
Conversation
|
Thanks for reviewing! Could a maintainer please add the \ label so the full test workflow runs on this non-draft PR? |
|
Follow-up: please add the |
|
Caution Review failedAn error occurred during the review process. Please try again later. 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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
common/time_utils.py (1)
49-51: ⚡ Quick winAdd logging for the new fallback branch.
The new “empty input → current timestamp” path is a behavior branch and should emit a debug log for observability.
💡 Proposed fix
def timestamp_to_date(timestamp, format_string="%Y-%m-%d %H:%M:%S"): @@ - if not timestamp: + if not timestamp: + logging.debug("timestamp_to_date received empty timestamp; using current_timestamp() fallback") timestamp = current_timestamp()As per coding guidelines, "
**/*.py: Add logging for new flows".🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@common/time_utils.py` around lines 49 - 51, The fallback branch in the timestamp handling (the `if not timestamp:` condition where `current_timestamp()` is called) lacks logging for observability. Add a debug log statement inside this conditional block after the `timestamp = current_timestamp()` assignment to log that the fallback to current timestamp occurred, ensuring the new flow path is observable for debugging and monitoring purposes.Source: Coding guidelines
test/unit_test/common/test_time_utils.py (1)
656-708: ⚡ Quick winAdd required priority markers to the new pytest classes.
Please tag these new regression classes with a priority marker (p1/p2/p3) to match test-selection conventions.
💡 Proposed fix
+@pytest.mark.p1 class TestTimestampToDateCurrentTimeFallback: @@ +@pytest.mark.p1 class TestFormatIso8601ToYmdHms:As per coding guidelines, "
test/**/*.py: Use pytest with priority markers (p1/p2/p3) for Python testing".🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/unit_test/common/test_time_utils.py` around lines 656 - 708, The new test classes TestTimestampToDateCurrentTimeFallback and TestFormatIso8601ToYmdHms are missing required pytest priority markers. Add a pytest marker decorator (using p1, p2, or p3) above each of these class definitions to indicate their priority level and match the test-selection conventions used throughout the test suite. Choose an appropriate priority level based on the importance and scope of each test class.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@common/time_utils.py`:
- Around line 49-51: In the `timestamp_to_date` function, the condition `if not
timestamp:` incorrectly treats the value 0 as missing input because 0 is falsy
in Python, causing valid epoch-millisecond timestamps to be silently replaced
with the current timestamp. Replace the condition `if not timestamp:` with `if
timestamp is None:` to explicitly check for None instead of relying on falsy
evaluation, allowing 0 to be properly treated as a valid timestamp input.
---
Nitpick comments:
In `@common/time_utils.py`:
- Around line 49-51: The fallback branch in the timestamp handling (the `if not
timestamp:` condition where `current_timestamp()` is called) lacks logging for
observability. Add a debug log statement inside this conditional block after the
`timestamp = current_timestamp()` assignment to log that the fallback to current
timestamp occurred, ensuring the new flow path is observable for debugging and
monitoring purposes.
In `@test/unit_test/common/test_time_utils.py`:
- Around line 656-708: The new test classes
TestTimestampToDateCurrentTimeFallback and TestFormatIso8601ToYmdHms are missing
required pytest priority markers. Add a pytest marker decorator (using p1, p2,
or p3) above each of these class definitions to indicate their priority level
and match the test-selection conventions used throughout the test suite. Choose
an appropriate priority level based on the importance and scope of each test
class.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e3c6011d-3023-4235-a5ae-54d9ed8c881e
📒 Files selected for processing (2)
common/time_utils.pytest/unit_test/common/test_time_utils.py
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
test/unit_test/common/test_time_utils.py (1)
667-681:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winMake current-time fallback assertions deterministic to avoid CI flakes.
These tests rely on real wall-clock bounds (±2s), which can intermittently fail under scheduler pauses. Mock
common.time_utils.current_timestampto a fixed millisecond value and assert exact output instead.💡 Proposed deterministic test update
class TestTimestampToDateCurrentTimeFallback: @@ - def test_none_uses_current_time(self): - """None input must resolve to the current time, not the epoch.""" - before = time.time() - result = timestamp_to_date(None) - after = time.time() - parsed = time.mktime(time.strptime(result, "%Y-%m-%d %H:%M:%S")) - assert before - 2 <= parsed <= after + 2 + def test_none_uses_current_time(self, monkeypatch): + """None input must resolve to current_timestamp() fallback.""" + fixed_ms = 1704067200123 + monkeypatch.setattr("common.time_utils.current_timestamp", lambda: fixed_ms) + assert timestamp_to_date(None) == time.strftime( + "%Y-%m-%d %H:%M:%S", time.localtime(fixed_ms / 1000) + ) @@ - def test_empty_string_uses_current_time(self): - """Empty-string input must resolve to the current time, not the epoch.""" - before = time.time() - result = timestamp_to_date("") - after = time.time() - parsed = time.mktime(time.strptime(result, "%Y-%m-%d %H:%M:%S")) - assert before - 2 <= parsed <= after + 2 + def test_empty_string_uses_current_time(self, monkeypatch): + """Empty-string input must resolve to current_timestamp() fallback.""" + fixed_ms = 1704067200123 + monkeypatch.setattr("common.time_utils.current_timestamp", lambda: fixed_ms) + assert timestamp_to_date("") == time.strftime( + "%Y-%m-%d %H:%M:%S", time.localtime(fixed_ms / 1000) + )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/unit_test/common/test_time_utils.py` around lines 667 - 681, The test methods test_none_uses_current_time and test_empty_string_uses_current_time are using real wall-clock time boundaries with ±2 second tolerances to verify that timestamp_to_date handles None and empty string inputs by returning the current time, but this approach is flaky under scheduler pauses. Mock the current_timestamp function from common.time_utils to return a fixed millisecond value, then update both test assertions to compare the result of timestamp_to_date against the exact expected output derived from the mocked timestamp rather than comparing against before/after time ranges. This makes the tests deterministic and eliminates timing-dependent failures.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@test/unit_test/common/test_time_utils.py`:
- Around line 667-681: The test methods test_none_uses_current_time and
test_empty_string_uses_current_time are using real wall-clock time boundaries
with ±2 second tolerances to verify that timestamp_to_date handles None and
empty string inputs by returning the current time, but this approach is flaky
under scheduler pauses. Mock the current_timestamp function from
common.time_utils to return a fixed millisecond value, then update both test
assertions to compare the result of timestamp_to_date against the exact expected
output derived from the mocked timestamp rather than comparing against
before/after time ranges. This makes the tests deterministic and eliminates
timing-dependent failures.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 10fc9ff3-86eb-4fcd-81ef-56354bae0857
📒 Files selected for processing (2)
common/time_utils.pytest/unit_test/common/test_time_utils.py
🚧 Files skipped from review as they are similar to previous changes (1)
- common/time_utils.py
|
Addressed the CodeRabbit flakiness feedback in the latest commit by making fallback assertions deterministic via current_timestamp monkeypatching (no wall-clock tolerance windows). |
|
Friendly follow-up: could a maintainer please add the |
|
Hi maintainers, quick follow-up on this one. The PR is non-draft and mergeable, but the test workflow is still skipped without the |
|
Quick update: this PR remains focused on the timestamp/ISO parsing fix and deterministic fallback tests. I can make any additional adjustments quickly if maintainers prefer a different shape; would appreciate a review when possible. |
|
Hi maintainers — please look into this when you have a chance. Happy to adjust if anything needs changing. |
|
Hi maintainers, this is ready for review when you have a chance. Happy to adjust if anything needs changing. |
|
Hi maintainers, this branch is current and CodeRabbit is passing. Could you add the |
1 similar comment
|
Hi maintainers, this branch is current and CodeRabbit is passing. Could you add the |
78db4e9 to
f86a0e7
Compare
a2801e2 to
6e82e27
Compare
|
This PR was closed after its branch was accidentally moved to the wrong commit (
I attempted to restore the PR directly, but GitHub currently blocks both recovery steps from my side:
The shortest recovery path is:
Once the branch points at that rebuilt chain, the PR should be recoverable normally. |
|
@yuzhichang Thanks for the recovery notes. I cherry-picked the three commits onto current |
… 8601 parsing (#16483) Recovery PR for #16173 after the fork branch was accidentally reset during rewrite-cleanup. Cherry-picked onto current `main`: - fix(common/time_utils): correct fallback timestamp and ISO-8601 normalization - fix(common/time_utils): preserve zero timestamps and mark regression tests - test(common/time_utils): make fallback assertions deterministic Supersedes closed #16173 — same branch `Harsh23Kashyap/fix/time-utils-edgecases`, rebuilt per @yuzhichang recovery steps in #16173 (comment) --------- Co-authored-by: Harsh Kashyap <[email protected]> Co-authored-by: Cursor <[email protected]>
What problem does this PR solve?
Fixes two correctness bugs in
common/time_utils.py:timestamp_to_date(None)/timestamp_to_date("")returned a date near 1970 instead of the current time because the fallback usedtime.time()(seconds) and then divided by 1000 again.format_iso_8601_to_ymd_hmsvalidated withdateutil.isoparsebut then re-parsed with stricterdatetime.fromisoformat, so ordinal ISO dates like2024-001T12:00:00Zwere returned unconverted.This change uses
current_timestamp()for the fallback and normalizes timestamps by parsing once withdateutil.isoparse. It also adds regression tests for None/empty fallback handling and ordinal ISO date parsing.Type of change
AI-assistance disclosure: parts of this change (bug triage and test scaffolding) were drafted with AI assistance and fully reviewed and verified by me.