Skip to content

Commit 7f099e7

Browse files
committed
fix(databricks): lazy-load the “workbench” profile
Defer loading of the default “workbench” credentials until they’re actually needed in `WorkbenchStrategy.__call__`. Previously we loaded the profile eagerly, which could raise if you never used it in a given context.
1 parent def37c7 commit 7f099e7

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/posit/workbench/external/databricks.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,30 @@ def text():
7171
"""
7272

7373
def __init__(self, config: Optional[Config] = None):
74-
self._config = config or Config(profile="workbench")
74+
self.__config = config
7575

7676
def auth_type(self) -> str:
7777
return POSIT_WORKBENCH_AUTH_TYPE
7878

79+
@property
80+
def _config(self) -> Config:
81+
"""The Databricks SDK `Config` object used by this strategy.
82+
83+
Returns
84+
-------
85+
Config
86+
The provided `Config` object, defaulting to a new `Config` with the profile set to "workbench" if not provided.
87+
"""
88+
if self.__config is None:
89+
# Do not create this configuration object until it is needed.
90+
# This avoids failing if the 'workbench' profile is not defined in the user's
91+
# `~/.databrickscfg` file until this strategy is actually used.
92+
self.__config = Config(profile="workbench")
93+
94+
return self.__config
95+
7996
def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002
8097
if self._config.token is None:
8198
raise ValueError("Missing value for field 'token' in Config.")
8299

83-
def cp():
84-
return {"Authorization": f"Bearer {self._config.token}"}
85-
86-
return cp
100+
return lambda: {"Authorization": f"Bearer {self._config.token}"}

tests/posit/workbench/external/test_databricks.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414

1515

1616
class TestPositCredentialsHelpers:
17-
def test_workbench_strategy(self):
18-
# default will attempt to load the workbench profile
17+
def test_default_workbench_strategy(self):
18+
# By default, the WorkbenchStrategy should use "workbench" profile.
19+
strategy = WorkbenchStrategy()
20+
1921
with pytest.raises(ValueError, match="profile=workbench"):
20-
WorkbenchStrategy()
22+
strategy()
2123

24+
def test_workbench_strategy(self):
2225
# providing a Config is allowed
2326
cs = WorkbenchStrategy(
2427
config=Config(host="https://databricks.com/workspace", token="token") # pyright: ignore[reportPossiblyUnboundVariable]

0 commit comments

Comments
 (0)