From 158266d41162f09d08534c1cfe5ba87b9844074d Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Sat, 9 Nov 2024 11:05:06 +0800 Subject: [PATCH] fix(providers): use version compare to decide whether to import asset --- .../src/airflow/providers/common/io/assets/file.py | 10 ++++++++-- .../src/airflow/providers/openlineage/utils/utils.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/providers/src/airflow/providers/common/io/assets/file.py b/providers/src/airflow/providers/common/io/assets/file.py index aeff818bd6ee..3bc307565aaa 100644 --- a/providers/src/airflow/providers/common/io/assets/file.py +++ b/providers/src/airflow/providers/common/io/assets/file.py @@ -19,9 +19,15 @@ import urllib.parse from typing import TYPE_CHECKING -try: +from packaging.version import Version + +from airflow import __version__ as AIRFLOW_VERSION + +# TODO: Remove version check block after bumpping common provider to 1.3.0 +AIRFLOW_V_3_0_PLUS = Version(Version(AIRFLOW_VERSION).base_version) >= Version("3.0.0") +if AIRFLOW_V_3_0_PLUS: from airflow.sdk.definitions.asset import Asset -except ModuleNotFoundError: +else: from airflow.datasets import Dataset as Asset # type: ignore[no-redef] if TYPE_CHECKING: diff --git a/providers/src/airflow/providers/openlineage/utils/utils.py b/providers/src/airflow/providers/openlineage/utils/utils.py index d1bb07eff441..d511224d5ddb 100644 --- a/providers/src/airflow/providers/openlineage/utils/utils.py +++ b/providers/src/airflow/providers/openlineage/utils/utils.py @@ -720,9 +720,15 @@ def translate_airflow_asset(asset: Asset, lineage_context) -> OpenLineageDataset This function returns None if no URI normalizer is defined, no asset converter is found or some core Airflow changes are missing and ImportError is raised. """ - try: + # TODO: Remove version check block after bumpping common provider to 1.3.0 + from packaging.version import Version + + from airflow import __version__ as AIRFLOW_VERSION + + AIRFLOW_V_3_0_PLUS = Version(Version(AIRFLOW_VERSION).base_version) >= Version("3.0.0") + if AIRFLOW_V_3_0_PLUS: from airflow.sdk.definitions.asset import _get_normalized_scheme - except ModuleNotFoundError: + else: try: from airflow.datasets import _get_normalized_scheme # type: ignore[no-redef, attr-defined] except ImportError: