Skip to content

fix(common): use ISO week/year in detect_datetime_format so week-date cursors round-trip - #4061

Merged
rudolfix merged 4 commits into
dlt-hub:develfrom
yashs33244:fix/iso-week-detect-datetime-format
Jun 19, 2026
Merged

fix(common): use ISO week/year in detect_datetime_format so week-date cursors round-trip#4061
rudolfix merged 4 commits into
dlt-hub:develfrom
yashs33244:fix/iso-week-detect-datetime-format

Conversation

@yashs33244

Copy link
Copy Markdown
Contributor

detect_datetime_format (dlt/common/time.py) mapped ISO 8601 week dates (YYYY-Www) to "%Y-W%W" / "%Y-W%W-%u". %W (locale week-of-year, tied to the calendar year %Y) disagrees with the ISO week %V + ISO week-numbering year %G at year boundaries.

The detected format is used by dlt/extract/incremental/lag.py to re-serialize a parsed-and-lagged string cursor, so it must round-trip — and it did not: "2026-W01" parsed to 2025-12-29 and rendered back as "2025-W52", silently corrupting the saved incremental cursor at year boundaries.

Fix: emit "%G-W%V" / "%G-W%V-%u".

Red-green proven: 9 tests fail without the fix. An exhaustive 2015–2030 round-trip scan shows 0 regressions and 303 previously-broken week values fixed; out-of-range weeks (e.g. W53 in a 52-week year) are still rejected at parse time. tests/common/test_time.py 361 passed, tests/extract/test_lag.py 426 passed. black --check, ruff check, flake8, and mypy are clean on the touched files.

Copilot AI review requested due to automatic review settings June 15, 2026 09:17

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 reached their quota limit.

rudolfix
rudolfix previously approved these changes Jun 15, 2026

@rudolfix rudolfix left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

detect_datetime_format mapped "YYYY-Www" to "%Y-W%W". pendulum parses
week strings as ISO 8601 week dates, but %W is the locale week-of-year
keyed on the calendar year %Y, which disagrees with the ISO week number
(%V) and ISO week-numbering year (%G) around year boundaries.

This breaks incremental cursors with a lag: a string cursor is parsed,
shifted, then re-serialized with the detected format. e.g. "2026-W01"
parses to 2025-12-29 and was rendered back as "2025-W52", silently
corrupting the saved cursor. "2020-W53" rendered as "2020-W52".

Use the ISO directives %G-W%V and %G-W%V-%u so the detected format
round-trips for every valid ISO week. Add a round-trip test in
test_time.py and boundary cases to test_apply_lag_to_str_value.
@yashs33244
yashs33244 force-pushed the fix/iso-week-detect-datetime-format branch from a4af372 to 1ae1e69 Compare June 15, 2026 11:48
@yashs33244

Copy link
Copy Markdown
Contributor Author

The failing checks look unrelated to this change, which only touches dlt/common/time.py (the ISO-week format strings) plus tests/common/test_time.py and tests/extract/test_lag.py:

  • Windows common tests (3.11 / 3.13) — the only failures are in tests/common/storages/test_local_filesystem.py::test_local_path_win_configuration (Windows file:// URL encoding), e.g. assert 'file://local…' == 'file://local…' on visually-identical strings. That suite has nothing to do with datetime-format detection, and the time/lag tests added here pass on Windows (the job reports 3 failed, 1668 passed, all 3 being those filesystem tests). The common | common tests check is just the matrix-rollup gate failing because of that Windows leg.
  • airflow 3 tests — a dlt_smoke DAG run (RuntimeError: DAG run failed); the smoke source doesn't exercise ISO-week cursors, so it's unaffected by this patch.
  • Linux (3.10–3.14) and macOS pass fully, including the new tests.

I've rebased onto the latest devel for a clean run. Happy to dig in further if any of these actually trace back to the change.

@rudolfix
rudolfix merged commit bffc2da into dlt-hub:devel Jun 19, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants