Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Bug Fixes

* FilesExt no longer retries on 500 (Internal Server Error) responses. These errors now fail immediately or fallback to alternative upload methods as appropriate.
* Fixed `Config` subclass attribute discovery and caching so inherited `ConfigAttribute` fields resolve correctly (addresses [#1069](https://github.com/databricks/databricks-sdk-py/issues/1069), [#1258](https://github.com/databricks/databricks-sdk-py/pull/1258)).

### Documentation

Expand Down
20 changes: 14 additions & 6 deletions databricks/sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,21 +596,29 @@ def clock(self) -> Clock:
@classmethod
def attributes(cls) -> Iterable[ConfigAttribute]:
"""Returns a list of Databricks SDK configuration metadata"""
if hasattr(cls, "_attributes"):
if "_attributes" in cls.__dict__:
return cls._attributes
if sys.version_info[1] >= 10:
import inspect

anno = inspect.get_annotations(cls)
get_annotations = inspect.get_annotations
else:
# Python 3.7 compatibility: getting type hints require extra hop, as described in
# "Accessing The Annotations Dict Of An Object In Python 3.9 And Older" section of
# https://docs.python.org/3/howto/annotations.html
anno = cls.__dict__["__annotations__"]
attrs = []
for name, v in cls.__dict__.items():
if type(v) != ConfigAttribute:
get_annotations = lambda class_obj: class_obj.__dict__.get("__annotations__", {})
anno = {}
attrs_by_name = {}
for class_obj in reversed(cls.mro()):
if class_obj is object:
continue
anno.update(get_annotations(class_obj))
for name, v in class_obj.__dict__.items():
if type(v) != ConfigAttribute:
continue
attrs_by_name[name] = v
attrs = []
for name, v in attrs_by_name.items():
v.name = name
v.transform = v._custom_transform if v._custom_transform else anno.get(name, str)
attrs.append(v)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ def __init__(self):
DatabricksConfig()


def test_config_subclass_inherits_attributes(monkeypatch):
# Force a fresh attribute resolution path on the subclass.
monkeypatch.delattr(Config, "_attributes", raising=False)

class DatabricksConfig(Config):
pass

cfg = DatabricksConfig(host="https://abc123.azuredatabricks.net", token="x")

assert cfg.host == "https://abc123.azuredatabricks.net"
assert cfg.token == "x"


def test_config_parsing_non_string_env_vars(monkeypatch):
monkeypatch.setenv("DATABRICKS_DEBUG_TRUNCATE_BYTES", "100")
c = Config(host="http://localhost", credentials_strategy=noop_credentials)
Expand Down