Skip to content

Commit

Permalink
#215 Improve access of __annotations__.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSpece committed Jul 18, 2023
1 parent 6f93548 commit c5a6867
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 8 additions & 4 deletions databricks/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,14 @@ def attributes(cls) -> Iterable[ConfigAttribute]:
""" Returns a list of Databricks SDK configuration metadata """
if hasattr(cls, '_attributes'):
return cls._attributes
# 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__']
if sys.version_info[1] >= 10:
import inspect
anno = inspect.get_annotations(cls)
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:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,19 @@ def test_config_accounts_dod_is_accounts_host(config):
def test_config_workspace_is_not_accounts_host(config):
config.host = "https://westeurope.azuredatabricks.net"
assert not config.is_account_client


def test_config_can_be_subclassed():

class DatabricksConfig(Config):

def __init__(self):
super().__init__()

with pytest.raises(ValueError): # As opposed to `KeyError`.
DatabricksConfig()


if __name__ == "__main__":
import conftest
test_config_accounts_dod_is_accounts_host(conftest.config.__pytest_wrapped__.obj())

0 comments on commit c5a6867

Please sign in to comment.