-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Implement timetables in SDK #58669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,414
−518
Merged
Implement timetables in SDK #58669
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0535ab9
Implement timetables in SDK
uranusjr 7157e40
Move timetable serde logic to airflow.serialization
uranusjr bf77015
Bring back validation in Core
uranusjr d343a40
Move NullAsset to SDK
uranusjr a3668a8
Simplify exception deprecation compat code
uranusjr ce9e6c1
Add validate() to SDK timetables
uranusjr 7d70413
Fix find_registered_custom_timetable() logic
uranusjr 4361911
Document timetable classes in sdk
uranusjr 54bff8b
Fix tests accessing sdk timetable internals
uranusjr 8aaadf7
Fix timetable inspection in OL integration
uranusjr 5cc398d
Fix croniter exception import for compatibility
uranusjr ffa4328
Fix tests to ensure correct timetable classes
uranusjr 6c4892a
SDK timetable classes have different field names
uranusjr c9c9c3d
Fix compat in Pytest plugin
uranusjr 69b3d4d
Rename SDK timetable.assets to asset_condition
uranusjr fcca60f
Rename internal function for clarity
uranusjr 380acb4
Fix stale docstring
uranusjr 5eab691
Move BaseTimetable to airflow.sdk.bases
uranusjr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import datetime | ||
| from typing import TYPE_CHECKING, Any, TypeVar | ||
|
|
||
| import dateutil.relativedelta | ||
|
|
||
| from airflow.sdk import ( # TODO: Implement serialized assets. | ||
| Asset, | ||
| AssetAlias, | ||
| AssetAll, | ||
| AssetAny, | ||
| ) | ||
| from airflow.serialization.definitions.assets import SerializedAssetWatcher | ||
| from airflow.serialization.enums import DagAttributeTypes as DAT, Encoding | ||
| from airflow.serialization.helpers import find_registered_custom_timetable, is_core_timetable_import_path | ||
| from airflow.utils.module_loading import import_string | ||
|
|
||
| if TYPE_CHECKING: | ||
| from airflow.sdk.definitions.asset import BaseAsset | ||
| from airflow.timetables.base import Timetable as CoreTimetable | ||
|
|
||
| R = TypeVar("R") | ||
|
|
||
|
|
||
| def decode_relativedelta(var: dict[str, Any]) -> dateutil.relativedelta.relativedelta: | ||
| """Dencode a relativedelta object.""" | ||
| if "weekday" in var: | ||
| var["weekday"] = dateutil.relativedelta.weekday(*var["weekday"]) | ||
| return dateutil.relativedelta.relativedelta(**var) | ||
|
|
||
|
|
||
| def decode_interval(value: int | dict) -> datetime.timedelta | dateutil.relativedelta.relativedelta: | ||
| if isinstance(value, dict): | ||
| return decode_relativedelta(value) | ||
| return datetime.timedelta(seconds=value) | ||
|
|
||
|
|
||
| def decode_run_immediately(value: bool | float) -> bool | datetime.timedelta: | ||
| if isinstance(value, float): | ||
| return datetime.timedelta(seconds=value) | ||
| return value | ||
|
|
||
|
|
||
| def smart_decode_trigger_kwargs(d): | ||
| """ | ||
| Slightly clean up kwargs for display or execution. | ||
|
|
||
| This detects one level of BaseSerialization and tries to deserialize the | ||
| content, removing some __type __var ugliness when the value is displayed | ||
| in UI to the user and/or while execution. | ||
| """ | ||
| from airflow.serialization.serialized_objects import BaseSerialization | ||
|
|
||
| if not isinstance(d, dict) or Encoding.TYPE not in d: | ||
| return d | ||
| return BaseSerialization.deserialize(d) | ||
|
|
||
|
|
||
| def decode_asset(var: dict[str, Any]): | ||
| watchers = var.get("watchers", []) | ||
| return Asset( | ||
| name=var["name"], | ||
| uri=var["uri"], | ||
| group=var["group"], | ||
| extra=var["extra"], | ||
| watchers=[ | ||
| SerializedAssetWatcher( | ||
| name=watcher["name"], | ||
| trigger={ | ||
| "classpath": watcher["trigger"]["classpath"], | ||
| "kwargs": smart_decode_trigger_kwargs(watcher["trigger"]["kwargs"]), | ||
| }, | ||
| ) | ||
| for watcher in watchers | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| def decode_asset_condition(var: dict[str, Any]) -> BaseAsset: | ||
| """ | ||
| Decode a previously serialized asset condition. | ||
|
|
||
| :meta private: | ||
| """ | ||
| match var["__type"]: | ||
| case DAT.ASSET: | ||
| return decode_asset(var) | ||
| case DAT.ASSET_ALL: | ||
| return AssetAll(*(decode_asset_condition(x) for x in var["objects"])) | ||
| case DAT.ASSET_ANY: | ||
| return AssetAny(*(decode_asset_condition(x) for x in var["objects"])) | ||
| case DAT.ASSET_ALIAS: | ||
| return AssetAlias(name=var["name"], group=var["group"]) | ||
| case DAT.ASSET_REF: | ||
| return Asset.ref(**{k: v for k, v in var.items() if k != "__type"}) | ||
| case data_type: | ||
| raise ValueError(f"deserialization not implemented for DAT {data_type!r}") | ||
|
|
||
|
|
||
| def decode_timetable(var: dict[str, Any]) -> CoreTimetable: | ||
| """ | ||
| Decode a previously serialized timetable. | ||
|
|
||
| Most of the deserialization logic is delegated to the actual type, which | ||
| we import from string. | ||
|
|
||
| :meta private: | ||
| """ | ||
| if is_core_timetable_import_path(importable_string := var[Encoding.TYPE]): | ||
| timetable_type: type[CoreTimetable] = import_string(importable_string) | ||
| else: | ||
| timetable_type = find_registered_custom_timetable(importable_string) | ||
| return timetable_type.deserialize(var[Encoding.VAR]) |
27 changes: 27 additions & 0 deletions
27
airflow-core/src/airflow/serialization/definitions/assets.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from airflow.sdk import AssetWatcher # TODO: Implement serialized assets. | ||
|
|
||
|
|
||
| class SerializedAssetWatcher(AssetWatcher): | ||
| """JSON serializable representation of an asset watcher.""" | ||
|
|
||
| trigger: dict |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.