Skip to content

Commit 1fd3b9e

Browse files
authored
Make WorkspaceClient and AccountClient more friendly with autospeccing (#480)
## Changes We cannot `create_autospec(WorkspaceClient)`, because all public properties are created within the constructor: ``` >>> WorkspaceClient <class 'databricks.sdk.WorkspaceClient'> >>> ws = create_autospec(WorkspaceClient) >>> ws <MagicMock spec='WorkspaceClient' id='4394200816'> >>> ws.workspace.list() Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec exec(exp, global_vars, local_vars) File "<input>", line 1, in <module> File "/opt/homebrew/Cellar/[email protected]/3.10.12_1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/unittest/mock.py", line 643, in __getattr__ raise AttributeError("Mock object has no attribute %r" % name) AttributeError: Mock object has no attribute 'workspace' ``` This PR makes it easier to introspect the class instances and makes mocks more reliable. All existing behavior stays the same. See: - https://docs.python.org/3/library/unittest.mock.html#autospeccing - https://stackoverflow.com/a/35921144/277035 ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [x] `make fmt` applied - [x] relevant integration tests applied
1 parent 02bbf0c commit 1fd3b9e

File tree

3 files changed

+615
-982
lines changed

3 files changed

+615
-982
lines changed

.codegen/__init__.py.tmpl

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,34 @@ class WorkspaceClient:
5252
debug_headers=debug_headers,
5353
product=product,
5454
product_version=product_version)
55-
self.config = config.copy()
56-
self.dbutils = _make_dbutils(self.config)
57-
self.api_client = client.ApiClient(self.config)
55+
self._config = config.copy()
56+
self._dbutils = _make_dbutils(self._config)
57+
self._api_client = client.ApiClient(self._config)
5858

5959
{{- range .Services}}{{if not .IsAccounts}}
60-
self.{{.SnakeName}}: {{template "api" .}} = {{template "api" .}}(self.api_client){{if .Description}}
61-
"""{{.Comment " " 100}}"""{{end}}
62-
{{end -}}{{end}}
60+
self._{{.SnakeName}} = {{template "api" .}}(self._api_client){{end -}}{{end}}
61+
62+
@property
63+
def config(self) -> client.Config:
64+
return self._config
65+
66+
@property
67+
def api_client(self) -> client.ApiClient:
68+
return self._api_client
69+
70+
@property
71+
def dbutils(self) -> dbutils.RemoteDbUtils:
72+
return self._dbutils
73+
74+
{{- range .Services}}{{if not .IsAccounts}}
75+
@property
76+
def {{.SnakeName}}(self) -> {{template "api" .}}:
77+
{{if .Description}}"""{{.Summary}}"""{{end}}
78+
return self._{{.SnakeName}}
79+
{{end -}}{{end}}
80+
81+
def __repr__(self):
82+
return f"WorkspaceClient(host='{self._config.host}', auth_type='{self._config.auth_type}', ...)"
6383

6484
class AccountClient:
6585
"""
@@ -80,10 +100,26 @@ class AccountClient:
80100
debug_headers=debug_headers,
81101
product=product,
82102
product_version=product_version)
83-
self.config = config.copy()
84-
self.api_client = client.ApiClient(self.config)
103+
self._config = config.copy()
104+
self._api_client = client.ApiClient(self._config)
85105

86106
{{- range .Services}}{{if .IsAccounts}}
87-
self.{{(.TrimPrefix "account").SnakeName}}: {{template "api" .}} = {{template "api" .}}(self.api_client){{if .Description}}
88-
"""{{.Comment " " 100}}"""{{end}}
89-
{{end -}}{{end}}
107+
self._{{(.TrimPrefix "account").SnakeName}} = {{template "api" .}}(self._api_client){{end -}}{{end}}
108+
109+
@property
110+
def config(self) -> client.Config:
111+
return self._config
112+
113+
@property
114+
def api_client(self) -> client.ApiClient:
115+
return self._api_client
116+
117+
{{- range .Services}}{{if .IsAccounts}}
118+
@property
119+
def {{(.TrimPrefix "account").SnakeName}}(self) -> {{template "api" .}}:{{if .Description}}
120+
"""{{.Summary}}"""{{end}}
121+
return self._{{(.TrimPrefix "account").SnakeName}}
122+
{{end -}}{{end}}
123+
124+
def __repr__(self):
125+
return f"AccountClient(account_id='{self._config.account_id}', auth_type='{self._config.auth_type}', ...)"

0 commit comments

Comments
 (0)