Uh oh!
There was an error while loading. Please reload this page.
Fix DateTimeSensor crash when target_time renders to a datetime - #70320
Conversation
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
target_time is a template field, so it is only rendered after __init__ runs. DateTimeSensor validated and normalized it in the constructor (acting on the un-rendered Jinja expression), and poke() parsed self.target_time directly with timezone.parse(). With a templated target_time and render_template_as_native_obj enabled, the field renders to a datetime and poke() raised "TypeError: 'DateTime' object cannot be cast as 'str'". Store target_time verbatim in the constructor and defer normalization and validation to _moment, the accessor that already runs after rendering; poke() now goes through it. related: apache#70296 Signed-off-by: 1fanwang <1fannnw@gmail.com>
The earlier refactor removed the datetime-to-ISO-string normalization that the constructor used to apply to target_time, leaving a datetime stored in the template field. Relocate that normalization to poke (the post-render execute path) so a datetime target_time is again stored as an ISO string, while __init__ keeps only the plain assignment. Signed-off-by: 1fanwang <1fannnw@gmail.com>
poke() is not reached by DateTimeSensorAsync, which overrides execute(); moving the datetime normalization into _moment (the shared post-render accessor) localizes a naive datetime for both the sync and async sensors, and drops the explanatory comments. Signed-off-by: 1fanwang <1fannnw@gmail.com>
2d99c66 to
0e4bc6aCompareDrop exemption entries that sibling burn-down PRs already resolved on main, which a stale rebase base had re-introduced. Net change to this file is only the DateTimeSensor removal. Signed-off-by: 1fanwang <1fannnw@gmail.com>
1f67ce4 to
2ffad8dCompareUh oh!
There was an error while loading. Please reload this page.
Backport failed to create: v3-3-test. View the failure log Run detailsNote: As of Merging PRs targeted for Airflow 3.X In matter of doubt please ask in #release-management Slack channel.
You can attempt to backport this manually by running: cherry_picker 3c99701 v3-3-testThis should apply the commit to the v3-3-test branch and leave the commit in conflict state marking After you have resolved the conflicts, you can continue the backport process by running: cherry_picker --continueIf you don't have cherry-picker installed, see the installation guide. |
target_timeis a template field, so it is only rendered after__init__runs.DateTimeSensorvalidated and normalized it in the constructor — acting on the un-rendered Jinja expression — andpoke()parsedself.target_timedirectly withtimezone.parse(). Whentarget_timeis templated and the Dag usesrender_template_as_native_obj=True, the field renders to adatetime, andpoke()crashed:The deferrable path already read the value through
_moment, which handles bothstranddatetime, so only the synchronous sensor was affected. The constructor now storestarget_timeverbatim, and the datetime→ISO-string normalization it used to do is relocated into _moment rather than dropped; self.target_time itself is never mutated._momentstill parses and validates the rendered value, and both sensors route through it.__init__only assigns the template field now, in line with the burn-down in #70296.related: #70296
Testing Done
Operator-level repro (real Dag, real render, real
poke) — before the fix:After the fix:
test_date_time.py: 13 passed. The two normalization guards —_momentresolves a native-rendered and a constructor-passed datetime to an aware datetime — fail on the pre-fix source and pass after.Was generative AI tooling used to co-author this PR?
Generated-by: GitHub Copilot CLI following the guidelines