Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions providers/yandex/src/airflow/providers/yandex/links/yq.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,12 @@

from typing import TYPE_CHECKING

from airflow.providers.yandex.version_compat import BaseOperatorLink, XCom

if TYPE_CHECKING:
from airflow.models import BaseOperator
from airflow.models.taskinstancekey import TaskInstanceKey
from airflow.providers.yandex.version_compat import BaseOperator, Context

try:
from airflow.sdk.definitions.context import Context
except ImportError:
# TODO: Remove once provider drops support for Airflow 2
from airflow.utils.context import Context

from airflow.providers.common.compat.version_compat import AIRFLOW_V_3_0_PLUS

if AIRFLOW_V_3_0_PLUS:
from airflow.sdk import BaseOperatorLink
from airflow.sdk.execution_time.xcom import XCom
else:
from airflow.models import XCom # type: ignore[no-redef]
from airflow.models.baseoperatorlink import BaseOperatorLink # type: ignore[no-redef]

XCOM_WEBLINK_KEY = "web_link"

Expand All @@ -50,4 +38,4 @@ def get_link(self, operator: BaseOperator, *, ti_key: TaskInstanceKey):

@staticmethod
def persist(context: Context, task_instance: BaseOperator, web_link: str) -> None:
task_instance.xcom_push(context, key=XCOM_WEBLINK_KEY, value=web_link)
context["ti"].xcom_push(key=XCOM_WEBLINK_KEY, value=web_link)
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING

from airflow.models import BaseOperator
from airflow.providers.yandex.hooks.dataproc import DataprocHook
from airflow.providers.yandex.version_compat import BaseOperator

if TYPE_CHECKING:
try:
from airflow.sdk.definitions.context import Context
except ImportError:
# TODO: Remove once provider drops support for Airflow 2
from airflow.utils.context import Context
from airflow.providers.yandex.version_compat import Context


@dataclass
Expand Down
8 changes: 2 additions & 6 deletions providers/yandex/src/airflow/providers/yandex/operators/yq.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any

from airflow.models import BaseOperator
from airflow.providers.yandex.hooks.yq import YQHook
from airflow.providers.yandex.links.yq import YQLink
from airflow.providers.yandex.version_compat import BaseOperator

if TYPE_CHECKING:
try:
from airflow.sdk.definitions.context import Context
except ImportError:
# TODO: Remove once provider drops support for Airflow 2
from airflow.utils.context import Context
from airflow.providers.yandex.version_compat import Context


class YQExecuteQueryOperator(BaseOperator):
Expand Down
54 changes: 54 additions & 0 deletions providers/yandex/src/airflow/providers/yandex/version_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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.
#
# NOTE! THIS FILE IS COPIED MANUALLY IN OTHER PROVIDERS DELIBERATELY TO AVOID ADDING UNNECESSARY
# DEPENDENCIES BETWEEN PROVIDERS. IF YOU WANT TO ADD CONDITIONAL CODE IN YOUR PROVIDER THAT DEPENDS
# ON AIRFLOW VERSION, PLEASE COPY THIS FILE TO THE ROOT PACKAGE OF YOUR PROVIDER AND IMPORT
# THOSE CONSTANTS FROM IT RATHER THAN IMPORTING THEM FROM ANOTHER PROVIDER OR TEST CODE
#
from __future__ import annotations


def get_base_airflow_version_tuple() -> tuple[int, int, int]:
from packaging.version import Version

from airflow import __version__

airflow_version = Version(__version__)
return airflow_version.major, airflow_version.minor, airflow_version.micro


AIRFLOW_V_3_0_PLUS = get_base_airflow_version_tuple() >= (3, 0, 0)
AIRFLOW_V_3_1_PLUS = get_base_airflow_version_tuple() >= (3, 1, 0)

# BaseOperator: Use 3.1+ due to xcom_push method missing in SDK BaseOperator 3.0.x
if AIRFLOW_V_3_1_PLUS:
from airflow.sdk import BaseOperator
else:
from airflow.models import BaseOperator

# Other SDK components: Available since 3.0+
if AIRFLOW_V_3_0_PLUS:
from airflow.sdk import BaseOperatorLink
from airflow.sdk.definitions.context import Context
from airflow.sdk.execution_time.xcom import XCom
else:
from airflow.models import BaseOperatorLink, XCom
from airflow.utils.context import Context


__all__ = ["AIRFLOW_V_3_0_PLUS", "BaseOperator", "BaseOperatorLink", "Context", "XCom"]
8 changes: 2 additions & 6 deletions providers/yandex/tests/unit/yandex/links/test_yq.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@

from airflow.models.taskinstance import TaskInstance
from airflow.providers.yandex.links.yq import YQLink
from airflow.providers.yandex.version_compat import XCom

from tests_common.test_utils.mock_operators import MockOperator
from tests_common.test_utils.version_compat import AIRFLOW_V_3_0_PLUS

if AIRFLOW_V_3_0_PLUS:
from airflow.sdk.execution_time.xcom import XCom
else:
from airflow.models import XCom # type: ignore[no-redef]

yandexcloud = pytest.importorskip("yandexcloud")


Expand All @@ -46,7 +42,7 @@ def test_persist():
value="g.com",
)
else:
ti.xcom_push.assert_called_once_with(key="web_link", value="g.com", execution_date=None)
ti.xcom_push.assert_called_once_with(key="web_link", value="g.com")


def test_default_link():
Expand Down
Loading