Skip to content

Commit

Permalink
Create a deepcopy of config when creating workspace client from accou…
Browse files Browse the repository at this point in the history
…nt client (#542)

## Changes
* We are creating a shallow copy of the config object when creating a ws
client from acc client. This leads to the config of acc client being
overridden by changes made for the workspace client. This PR makes it so
that we are making a deepcopy instead.
* Also, correctly handle list response jsons in the API client. 

## Tests
* added integration test

- [ ] `make test` run locally
- [ ] `make fmt` applied
- [ ] relevant integration tests applied
  • Loading branch information
kartikgupta-db authored Feb 12, 2024
1 parent 85ba774 commit 9af2630
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion databricks/sdk/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions databricks/sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,8 @@ def copy(self):
cpy: Config = copy.copy(self)
cpy._user_agent_other_info = copy.deepcopy(self._user_agent_other_info)
return cpy

def deep_copy(self):
"""Creates a deep copy of the config object.
"""
return copy.deepcopy(self)
7 changes: 6 additions & 1 deletion databricks/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ def do(self,
resp[header] = response.headers.get(Casing.to_header_case(header))
if not len(response.content):
return resp
return {**resp, **response.json()}

json = response.json()
if isinstance(json, list):
return json

return {**resp, **json}

@staticmethod
def _is_retryable(err: BaseException) -> Optional[str]:
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ def test_get_workspace_client(a):
def test_get_workspace_id(ucws, env_or_skip):
ws_id = int(env_or_skip('THIS_WORKSPACE_ID'))
assert ucws.get_workspace_id() == ws_id


def test_creating_ws_client_from_ac_client_does_not_override_config(a):
wss = list(a.workspaces.list())
if len(wss) == 0:
pytest.skip("no workspaces")
a.get_workspace_client(wss[0])

# assert doesn't throw
wss = list(a.workspaces.list())

0 comments on commit 9af2630

Please sign in to comment.