Skip to content

Move all BaseHook usages in providers to version_compat #52676

@amoghrajesh

Description

@amoghrajesh

✅ Goal

Ensure that BaseHook is imported through version_compat.py to support both Airflow 2 and 3 cleanly, and avoid mypy issues.

Steps

Step 0: Grab the provider (file under it also ok) that you are interested in by commenting on this issue. DO NOT WORK ON A PROVIDER / TASK WITHOUT commenting, as it can cause conflict of PRs.

Step 1: Add BaseHook to version_compat.py

Update/Create the existing version_compat.py file in your provider (found under providers/{provider_name}/src/airflow/providers/{provider_name}/version_compat.py), to introduce a AIRFLOW_V_3_1_PLUS check like below:

--- a/providers/standard/src/airflow/providers/standard/version_compat.py
+++ b/providers/standard/src/airflow/providers/standard/version_compat.py
@@ -35,6 +35,11 @@ def get_base_airflow_version_tuple() -> tuple[int, int, int]:
 +AIRFLOW_V_3_1_PLUS: bool = get_base_airflow_version_tuple() >= (3, 1, 0)
 
+if AIRFLOW_V_3_1_PLUS:
+    from airflow.sdk import BaseHook
+else:
+    from airflow.hooks.base import BaseHook  # type: ignore[attr-defined,no-redef]

If the version_compat.py doesn't exist for that provider, create one with a template like so:

# In providers/{provider_name}/src/airflow/providers/{provider_name}/version_compat.py
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: bool = get_base_airflow_version_tuple() >= (3, 1, 0)

if AIRFLOW_V_3_1_PLUS:
    from airflow.sdk import BaseHook
else:
    from airflow.hooks.base import BaseHook  # type: ignore[attr-defined,no-redef]
    

if AIRFLOW_V_3_0_PLUS:
    from airflow.sdk import BaseOperator
else:
    from airflow.models import BaseOperator

__all__ = ["AIRFLOW_V_3_0_PLUS", "BaseHook", "BaseOperator"]

Note: Export only what's needed in __all__.

Step 2: Replace the imports

Replace all direct imports like:

try:
    from airflow.sdk import BaseHook
except ImportError:
    from airflow.hooks.base import BaseHook  # type: ignore[attr-defined,no-redef]

With that from version_compat:

from airflow.providers.standard.version_compat import BaseHook

Step 3: Run Tests

Run the provider tests using:

breeze testing providers-tests --test-type "Providers[{provider_name}]"

Example:

breeze testing providers-tests --test-type "Providers[microsoft.azure]"

Step 4: Open a PR

Providers that need changes

Airbyte

Alibaba Cloud @sunank200

Amazon AWS @sunank200

Apache (@bdsoha)

Apprise @sunank200

ArangoDB @sunank200

Asana

Atlassian @sunank200

Cloudant @sunank200

CNCF Kubernetes

Cohere @sunank200

Common SQL @sunank200

Datadog @sunank200

Docker @sunank200

Edge3: Not applicable, example dags should not be changed.

~- [ ] providers/edge3/src/airflow/providers/edge3/example_dags/integration_test.py
- [ ] providers/edge3/src/airflow/providers/edge3/example_dags/win_test.py

Elasticsearch @sunank200

Facebook @sunank200

FTP @sunank200

Git @amoghrajesh

GitHub @sunank200

Google @fweilun

gRPC @sunank200

HashiCorp @sunank200

HTTP @sunank200

IMAP @sunank200

InfluxDB @sunank200

Jenkins @sunank200

Microsoft Azure @fweilun

Microsoft PSRP

Microsoft WinRM

MongoDB

OpenAI @mingdaoy

OpenFaaS @amoghrajesh

OpenSearch @kyungjunleeme

Opsgenie

PagerDuty @amoghrajesh

Papermill @amoghrajesh

Pinecone @amoghrajesh

Qdrant @amoghrajesh

Redis @dominikhei

Salesforce @amoghrajesh

Samba @amoghrajesh

Segment @amoghrajesh

SendGrid @amoghrajesh

SFTP @sjyangkevin

Slack @amoghrajesh

SMTP @amoghrajesh

SSH @sunank200

Standard @amoghrajesh

Tableau @dominikhei

Telegram @sunank200

Teradata @phanikumv

Weaviate @phanikumv

Yandex @phanikumv

Zendesk @phanikumv

TLDR of how to contribute:

  1. Pick an unchecked provider from the list above
  2. Comment on this issue to claim it (prevents duplicate work)
  3. Follow the steps 1 - 4
  4. Submit a PR
  5. Check off the completed provider in this issue if you have access

Committer

  • I acknowledge that I am a maintainer/committer of the Apache Airflow project.

Metadata

Metadata

Assignees

Labels

area:providerskind:metaHigh-level information important to the community

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions