Skip to content

Commit cbdae71

Browse files
vuanhphungIsaac
andcommitted
refactor: write the new lazy-name lookups the same way
auth, authenticators and the cloudfetch downloader now use the same _LAZY_NAMES map as telemetry_client instead of if-chains. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
1 parent f4839f1 commit cbdae71

3 files changed

Lines changed: 32 additions & 19 deletions

File tree

‎src/databricks/sql/auth/auth.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
from typing import Any, Optional, List, TYPE_CHECKING
23

34
from databricks.sql.auth.authenticators import (
@@ -140,11 +141,15 @@ def get_python_sql_connector_auth_provider(hostname: str, http_client, **kwargs)
140141
return get_auth_provider(cfg, http_client)
141142

142143

144+
_LAZY_NAMES = {
145+
"TokenFederationProvider": "databricks.sql.auth.token_federation",
146+
}
147+
148+
143149
def __getattr__(name: str) -> Any:
144150
# Imported on first use rather than at load, so connect() skips them;
145151
# still importable, and patchable here, as module attributes.
146-
if name in ("TokenFederationProvider",):
147-
import databricks.sql.auth.token_federation as _source
148-
149-
return getattr(_source, name)
150-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
152+
source = _LAZY_NAMES.get(name)
153+
if source is None:
154+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
155+
return getattr(importlib.import_module(source), name)

‎src/databricks/sql/auth/authenticators.py‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
import abc
23
import logging
34
from typing import Any, Callable, Dict, List, TYPE_CHECKING
@@ -244,15 +245,17 @@ def header_factory() -> Dict[str, str]:
244245
return header_factory
245246

246247

248+
_LAZY_NAMES = {
249+
"OAuthManager": "databricks.sql.auth.oauth",
250+
"RefreshableTokenSource": "databricks.sql.auth.oauth",
251+
"ClientCredentialsTokenSource": "databricks.sql.auth.oauth",
252+
}
253+
254+
247255
def __getattr__(name: str) -> Any:
248256
# Imported on first use rather than at load, so connect() skips them;
249257
# still importable, and patchable here, as module attributes.
250-
if name in (
251-
"OAuthManager",
252-
"RefreshableTokenSource",
253-
"ClientCredentialsTokenSource",
254-
):
255-
import databricks.sql.auth.oauth as _source
256-
257-
return getattr(_source, name)
258-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
258+
source = _LAZY_NAMES.get(name)
259+
if source is None:
260+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
261+
return getattr(importlib.import_module(source), name)

‎src/databricks/sql/cloudfetch/downloader.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import importlib
34
import logging
45
from dataclasses import dataclass
56
from typing import Any, Optional, TYPE_CHECKING
@@ -23,14 +24,18 @@
2324
logger = logging.getLogger(__name__)
2425

2526

27+
_LAZY_NAMES = {
28+
"UnifiedHttpClient": "databricks.sql.common.unified_http_client",
29+
}
30+
31+
2632
def __getattr__(name: str) -> Any:
2733
# Imported on first use rather than at load, so connect() skips them;
2834
# still importable, and patchable here, as module attributes.
29-
if name in ("UnifiedHttpClient",):
30-
import databricks.sql.common.unified_http_client as _source
31-
32-
return getattr(_source, name)
33-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
35+
source = _LAZY_NAMES.get(name)
36+
if source is None:
37+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
38+
return getattr(importlib.import_module(source), name)
3439

3540

3641
@dataclass

0 commit comments

Comments
 (0)