Skip to content

Move BaseHook class to task SDK #51873

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

amoghrajesh
Copy link
Contributor

@amoghrajesh amoghrajesh commented Jun 18, 2025

closes: #51672

Moving the BaseHook class into task SDK, exactly where it should live similar to other base classes in sdk like notifiers, operators etc.

Testing: Running by all the methods defined on BaseHook -- old vs new

Running with the new path

DAG:

from __future__ import annotations

from contextlib import suppress

from airflow.models.baseoperator import BaseOperator
from airflow import DAG
from airflow.sdk import BaseHook


class CustomOperator(BaseOperator):
    def execute(self, context):
        conn = BaseHook.get_connection("athena_default")
        print("got connection from basehook", conn)


        hook = BaseHook.get_hook("athena_default")
        print("got hook from basehook", hook)

        with suppress(NotImplementedError):
            BaseHook.get_conn(hook)
            print("Raising a NotImplementedError, trying to access get_conn")

        logger = BaseHook.logger()
        print("default logger is", logger)


with DAG("get_connection_basehook", schedule=None, catchup=False) as dag:
    CustomOperator(task_id="set_var")

image

Running with the older path to check backcompat

from __future__ import annotations

from contextlib import suppress

from airflow.models.baseoperator import BaseOperator
from airflow import DAG
from airflow.hooks.base import BaseHook


class CustomOperator(BaseOperator):
    def execute(self, context):
        conn = BaseHook.get_connection("athena_default")
        print("got connection from basehook", conn)


        hook = BaseHook.get_hook("athena_default")
        print("got hook from basehook", hook)

        with suppress(NotImplementedError):
            BaseHook.get_conn(hook)
            print("Raising a NotImplementedError, trying to access get_conn")

        logger = BaseHook.logger()
        print("default logger is", logger)


with DAG("get_connection_basehook", schedule=None, catchup=False) as dag:
    CustomOperator(task_id="set_var")

image


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

from airflow.models import Connection
from airflow.providers_manager import ProvidersManager
from airflow.sdk import BaseHook
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Thors only needed for test conn? If so can we delay the import to the fn, and handle it missing with a nice error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a similar thought and tried implementing it with Connection instead: #51834

@@ -26,7 +26,7 @@
from requests import Session

from airflow.exceptions import AirflowException
from airflow.hooks.base import BaseHook
from airflow.sdk import BaseHook
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these will need a try/except so they continue to work on Airflow 2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let me make that change!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a change via: b544139

@amoghrajesh amoghrajesh requested a review from ashb June 18, 2025 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move BaseHook to TaskSDK under airflow.sdk.bases
2 participants