Skip to content

Commit 7d6f0c2

Browse files
authored
Make SQLAlchemy optional dependency for vertica Provider (#60177)
* add sqlalchemy as an optional dependency in vertica provider * add dependency in dev group and fix mypy
1 parent bc133ea commit 7d6f0c2

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

providers/vertica/pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ dependencies = [
6363
"vertica-python>=1.3.0",
6464
]
6565

66+
# The optional dependencies should be modified in place in the generated file
67+
# Any change in the dependencies is preserved when the file is regenerated
68+
[project.optional-dependencies]
69+
sqlalchemy = [
70+
"sqlalchemy>=1.4.49",
71+
]
72+
6673
[dependency-groups]
6774
dev = [
6875
"apache-airflow",
@@ -71,6 +78,7 @@ dev = [
7178
"apache-airflow-providers-common-sql",
7279
# Additional devel dependencies (do not remove this line and add extra development dependencies)
7380
"apache-airflow-providers-common-sql[pandas,polars]",
81+
"apache-airflow-providers-vertica[sqlalchemy]"
7482
]
7583

7684
# To build docs:

providers/vertica/src/airflow/providers/vertica/hooks/vertica.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
from __future__ import annotations
1818

1919
from collections.abc import Callable, Iterable, Mapping
20-
from typing import Any, overload
20+
from typing import TYPE_CHECKING, Any, overload
2121

22-
from sqlalchemy.engine import URL
2322
from vertica_python import connect
2423

24+
from airflow.exceptions import AirflowOptionalProviderFeatureException
2525
from airflow.providers.common.sql.hooks.handlers import fetch_all_handler
2626
from airflow.providers.common.sql.hooks.sql import DbApiHook
2727

28+
if TYPE_CHECKING:
29+
from sqlalchemy.engine import URL
30+
2831

2932
def vertica_fetch_all_handler(cursor) -> list[tuple] | None:
3033
"""
@@ -137,6 +140,13 @@ def get_conn(self) -> connect:
137140
@property
138141
def sqlalchemy_url(self) -> URL:
139142
"""Return a SQLAlchemy URL object with properly formatted query parameters."""
143+
try:
144+
from sqlalchemy.engine import URL
145+
except (ImportError, ModuleNotFoundError) as err:
146+
raise AirflowOptionalProviderFeatureException(
147+
"The 'sqlalchemy' library is required to use this feature. "
148+
"Please install it with: pip install 'apache-airflow-providers-vertica[sqlalchemy]'"
149+
) from err
140150
conn = self.get_connection(self.get_conn_id())
141151
extra = conn.extra_dejson or {}
142152

0 commit comments

Comments
 (0)