Skip to content

Commit 2f1809d

Browse files
authored
Apply attribute transformer when reading in attributes from the environment (#293)
## Changes Attributes read from the environment are not currently parsed before being stored in the `_inner` field of `Config`. As a result, the type of the attribute will be different depending on whether the attribute is read from the environment or passed to the constructor. This PR changes all argument parsing to use `__setattr__` which converts the argument to the expected type. Closes #292. ## Tests Unit test to ensure that string env vars are parsed to appropriate type for the affected field. - [ ] `make test` run locally - [ ] `make fmt` applied - [ ] relevant integration tests applied
1 parent d878914 commit 2f1809d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

databricks/sdk/core.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,20 +754,19 @@ def _set_inner_config(self, keyword_args: Dict[str, any]):
754754
continue
755755
if keyword_args.get(attr.name, None) is None:
756756
continue
757-
# make sure that args are of correct type
758-
self._inner[attr.name] = attr.transform(keyword_args[attr.name])
757+
self.__setattr__(attr.name, keyword_args[attr.name])
759758

760759
def _load_from_env(self):
761760
found = False
762-
for attr in Config.attributes():
761+
for attr in self.attributes():
763762
if not attr.env:
764763
continue
765764
if attr.name in self._inner:
766765
continue
767766
value = os.environ.get(attr.env)
768767
if not value:
769768
continue
770-
self._inner[attr.name] = value
769+
self.__setattr__(attr.name, value)
771770
found = True
772771
if found:
773772
logger.debug('Loaded from environment')

tests/test_core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
databricks_cli)
1212
from databricks.sdk.version import __version__
1313

14+
from .conftest import noop_credentials
15+
1416

1517
def test_parse_dsn():
1618
cfg = Config.parse_dsn('databricks://user:[email protected]?retry_timeout_seconds=600')
@@ -220,3 +222,9 @@ def __init__(self):
220222

221223
with pytest.raises(ValueError): # As opposed to `KeyError`.
222224
DatabricksConfig()
225+
226+
227+
def test_config_parsing_non_string_env_vars(monkeypatch):
228+
monkeypatch.setenv('DATABRICKS_DEBUG_TRUNCATE_BYTES', '100')
229+
c = Config(host='http://localhost', credentials_provider=noop_credentials)
230+
assert c.debug_truncate_bytes == 100

0 commit comments

Comments
 (0)