Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve access of __annotations__ #239

Merged
merged 5 commits into from
Jul 26, 2023
Merged
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
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
11 changes: 11 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,14 @@ 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()
Loading