-
Notifications
You must be signed in to change notification settings - Fork 312
Centralize version-aware EmptyOperator imports in a compatibility module #2758
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
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5324482
Use a version-aware EmptyOperator import path via a shared constant
pankajkoti fe4c60d
Centralize version-aware EmptyOperator imports in a compatibility module
pankajkoti 93b8b57
Compare on Airflow major version and cache lazy imports
pankajkoti b91b95f
Resolve EmptyOperator via a direct version-aware import
pankajkoti c0830b2
Exclude the unreachable provider-missing guard from coverage
pankajkoti 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """Version-aware imports for Airflow objects whose import path differs across Airflow 2 and 3. | ||
|
|
||
| Names exported here are resolved lazily on first attribute access (PEP 562), so importing | ||
| this module is free: the underlying Airflow object is only imported when the name is actually | ||
| used. Use sites keep referencing the object by its real name (e.g. ``EmptyOperator``) rather | ||
| than a version-specific dotted path. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from cosmos.constants import _AIRFLOW3_MAJOR_VERSION, AIRFLOW_VERSION | ||
|
|
||
| if TYPE_CHECKING: | ||
| # Resolved for type checkers / IDEs only; no import happens at runtime here. The standard | ||
| # provider path is tried first so the type resolves to the real class on Airflow 3 (the | ||
| # legacy module is a deprecated shim typed as ``Any`` there). | ||
| try: | ||
| from airflow.providers.standard.operators.empty import EmptyOperator as EmptyOperator | ||
| except ImportError: | ||
| from airflow.operators.empty import EmptyOperator as EmptyOperator # type: ignore[no-redef] | ||
|
|
||
| # Single source of truth for where ``EmptyOperator`` lives. The operator moved to the standard | ||
| # provider in Airflow 3; the legacy ``airflow.operators.empty`` path still resolves there but | ||
| # emits a ``DeprecatedImportWarning``, so on Airflow 3 we select the standard provider path. | ||
| # Compare on the major version so Airflow 3 pre-releases (e.g. 3.0.0rc1) are treated as Airflow 3. | ||
| _EMPTY_OPERATOR_MODULE = ( | ||
| "airflow.operators.empty" | ||
| if AIRFLOW_VERSION.major < _AIRFLOW3_MAJOR_VERSION | ||
| else "airflow.providers.standard.operators.empty" | ||
| ) | ||
|
|
||
| # Maps the exported name to the module it should be imported from for this Airflow version. | ||
| _LAZY_IMPORTS = { | ||
| "EmptyOperator": _EMPTY_OPERATOR_MODULE, | ||
| } | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> object: | ||
| module_path = _LAZY_IMPORTS.get(name) | ||
| if module_path is None: | ||
| raise AttributeError(f"module {__name__!r} has no attribute {name!r}") | ||
| # Cache the resolved symbol in the module namespace so subsequent attribute access skips | ||
| # __getattr__ entirely (PEP 562 only invokes it for names missing from globals()). | ||
| resolved = getattr(importlib.import_module(module_path), name) | ||
| globals()[name] = resolved | ||
| return resolved | ||
|
|
||
|
|
||
| def get_version_aware_operator_class_path(operator: type) -> str: | ||
| """Return the fully qualified import path for the given operator class. | ||
|
|
||
| The path is read from the class itself (``__module__`` + ``__name__``), so it reflects | ||
| the actual module the class lives in for the running Airflow version. Pair it with the | ||
| version-aware classes exported from this module, e.g.:: | ||
|
|
||
| get_version_aware_operator_class_path(EmptyOperator) | ||
|
|
||
| Used where Cosmos stores the operator as a dotted string for later dynamic import | ||
| (e.g. ``Task.operator_class``) instead of referencing the class directly. | ||
| """ | ||
| return f"{operator.__module__}.{operator.__name__}" | ||
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
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.