Skip to content

Commit 1a4119e

Browse files
committed
[change] Removed unused _current_client_context var
1 parent b1300f6 commit 1a4119e

File tree

6 files changed

+73
-149
lines changed

6 files changed

+73
-149
lines changed

pyatlan/client/atlan.py

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,6 @@ def get_session():
150150

151151

152152
class AtlanClient(BaseSettings):
153-
_current_client_ctx: ClassVar[ContextVar] = ContextVar(
154-
"_current_client_ctx", default=None
155-
)
156153
_401_has_retried_ctx: ClassVar[ContextVar] = ContextVar(
157154
"_401_has_retried_ctx", default=False
158155
)
@@ -195,25 +192,6 @@ class AtlanClient(BaseSettings):
195192
class Config:
196193
env_prefix = "atlan_"
197194

198-
@classmethod
199-
def set_current_client(cls, client: AtlanClient):
200-
"""
201-
Sets the current client context
202-
"""
203-
if not isinstance(client, AtlanClient):
204-
raise ErrorCode.MISSING_ATLAN_CLIENT.exception_with_parameters()
205-
cls._current_client_ctx.set(client)
206-
207-
@classmethod
208-
def get_current_client(cls) -> AtlanClient:
209-
"""
210-
Retrieves the current client context
211-
"""
212-
client = cls._current_client_ctx.get()
213-
if not client:
214-
raise ErrorCode.NO_ATLAN_CLIENT_AVAILABLE.exception_with_parameters()
215-
return client
216-
217195
def __init__(self, **data):
218196
super().__init__(**data)
219197
self._request_params = {
@@ -225,188 +203,161 @@ def __init__(self, **data):
225203
adapter = HTTPAdapter(max_retries=self.retry)
226204
session.mount(HTTPS_PREFIX, adapter)
227205
session.mount(HTTP_PREFIX, adapter)
228-
AtlanClient.set_current_client(self)
229206
self._401_has_retried_ctx.set(False)
230207

231208
@property
232209
def admin(self) -> AdminClient:
233210
if self._admin_client is None:
234-
AtlanClient.set_current_client(self)
235211
self._admin_client = AdminClient(client=self)
236212
return self._admin_client
237213

238214
@property
239215
def audit(self) -> AuditClient:
240216
if self._audit_client is None:
241-
AtlanClient.set_current_client(self)
242217
self._audit_client = AuditClient(client=self)
243218
return self._audit_client
244219

245220
@property
246221
def search_log(self) -> SearchLogClient:
247222
if self._search_log_client is None:
248-
AtlanClient.set_current_client(self)
249223
self._search_log_client = SearchLogClient(client=self)
250224
return self._search_log_client
251225

252226
@property
253227
def workflow(self) -> WorkflowClient:
254228
if self._workflow_client is None:
255-
AtlanClient.set_current_client(self)
256229
self._workflow_client = WorkflowClient(client=self)
257230
return self._workflow_client
258231

259232
@property
260233
def credentials(self) -> CredentialClient:
261234
if self._credential_client is None:
262-
AtlanClient.set_current_client(self)
263235
self._credential_client = CredentialClient(client=self)
264236
return self._credential_client
265237

266238
@property
267239
def group(self) -> GroupClient:
268240
if self._group_client is None:
269-
AtlanClient.set_current_client(self)
270241
self._group_client = GroupClient(client=self)
271242
return self._group_client
272243

273244
@property
274245
def role(self) -> RoleClient:
275246
if self._role_client is None:
276-
AtlanClient.set_current_client(self)
277247
self._role_client = RoleClient(client=self)
278248
return self._role_client
279249

280250
@property
281251
def asset(self) -> AssetClient:
282252
if self._asset_client is None:
283-
AtlanClient.set_current_client(self)
284253
self._asset_client = AssetClient(client=self)
285254
return self._asset_client
286255

287256
@property
288257
def impersonate(self) -> ImpersonationClient:
289258
if self._impersonate_client is None:
290-
AtlanClient.set_current_client(self)
291259
self._impersonate_client = ImpersonationClient(client=self)
292260
return self._impersonate_client
293261

294262
@property
295263
def queries(self) -> QueryClient:
296264
if self._query_client is None:
297-
AtlanClient.set_current_client(self)
298265
self._query_client = QueryClient(client=self)
299266
return self._query_client
300267

301268
@property
302269
def token(self) -> TokenClient:
303270
if self._token_client is None:
304-
AtlanClient.set_current_client(self)
305271
self._token_client = TokenClient(client=self)
306272
return self._token_client
307273

308274
@property
309275
def typedef(self) -> TypeDefClient:
310276
if self._typedef_client is None:
311-
AtlanClient.set_current_client(self)
312277
self._typedef_client = TypeDefClient(client=self)
313278
return self._typedef_client
314279

315280
@property
316281
def user(self) -> UserClient:
317282
if self._user_client is None:
318-
AtlanClient.set_current_client(self)
319283
self._user_client = UserClient(client=self)
320284
return self._user_client
321285

322286
@property
323287
def tasks(self) -> TaskClient:
324288
if self._task_client is None:
325-
AtlanClient.set_current_client(self)
326289
self._task_client = TaskClient(client=self)
327290
return self._task_client
328291

329292
@property
330293
def sso(self) -> SSOClient:
331294
if self._sso_client is None:
332-
AtlanClient.set_current_client(self)
333295
self._sso_client = SSOClient(client=self)
334296
return self._sso_client
335297

336298
@property
337299
def open_lineage(self) -> OpenLineageClient:
338300
if self._open_lineage_client is None:
339-
AtlanClient.set_current_client(self)
340301
self._open_lineage_client = OpenLineageClient(client=self)
341302
return self._open_lineage_client
342303

343304
@property
344305
def files(self) -> FileClient:
345306
if self._file_client is None:
346-
AtlanClient.set_current_client(self)
347307
self._file_client = FileClient(client=self)
348308
return self._file_client
349309

350310
@property
351311
def contracts(self) -> ContractClient:
352312
if self._contract_client is None:
353-
AtlanClient.set_current_client(self)
354313
self._contract_client = ContractClient(client=self)
355314
return self._contract_client
356315

357316
@property
358317
def atlan_tag_cache(self) -> AtlanTagCache:
359318
if self._atlan_tag_cache is None:
360-
AtlanClient.set_current_client(self)
361319
self._atlan_tag_cache = AtlanTagCache(client=self)
362320
return self._atlan_tag_cache
363321

364322
@property
365323
def enum_cache(self) -> EnumCache:
366324
if self._enum_cache is None:
367-
AtlanClient.set_current_client(self)
368325
self._enum_cache = EnumCache(client=self)
369326
return self._enum_cache
370327

371328
@property
372329
def group_cache(self) -> GroupCache:
373330
if self._group_cache is None:
374-
AtlanClient.set_current_client(self)
375331
self._group_cache = GroupCache(client=self)
376332
return self._group_cache
377333

378334
@property
379335
def role_cache(self) -> RoleCache:
380336
if self._role_cache is None:
381-
AtlanClient.set_current_client(self)
382337
self._role_cache = RoleCache(client=self)
383338
return self._role_cache
384339

385340
@property
386341
def user_cache(self) -> UserCache:
387342
if self._user_cache is None:
388-
AtlanClient.set_current_client(self)
389343
self._user_cache = UserCache(client=self)
390344
return self._user_cache
391345

392346
@property
393347
def custom_metadata_cache(self) -> CustomMetadataCache:
394348
if self._custom_metadata_cache is None:
395-
AtlanClient.set_current_client(self)
396349
self._custom_metadata_cache = CustomMetadataCache(client=self)
397350
return self._custom_metadata_cache
398351

399352
@property
400353
def connection_cache(self) -> ConnectionCache:
401354
if self._connection_cache is None:
402-
AtlanClient.set_current_client(self)
403355
self._connection_cache = ConnectionCache(client=self)
404356
return self._connection_cache
405357

406358
@property
407359
def source_tag_cache(self) -> SourceTagCache:
408360
if self._source_tag_cache is None:
409-
AtlanClient.set_current_client(self)
410361
self._source_tag_cache = SourceTagCache(client=self)
411362
return self._source_tag_cache
412363

@@ -1845,31 +1796,29 @@ def max_retries(
18451796

18461797
@contextlib.contextmanager
18471798
def client_connection(
1799+
client: AtlanClient,
18481800
base_url: Optional[HttpUrl] = None,
18491801
api_key: Optional[str] = None,
18501802
connect_timeout: float = 30.0,
18511803
read_timeout: float = 120.0,
18521804
retry: Retry = DEFAULT_RETRY,
18531805
) -> Generator[AtlanClient, None, None]:
18541806
"""
1855-
Creates a new client created with the given base_url and/api_key. The AtlanClient.default_client will
1856-
be set to the new client. AtlanClient.default_client will be reset to the current default_client before
1857-
exiting the context.
1858-
:param base_url: the base_url to be used for the new connection. If not specified the current value will be used
1859-
:param api_key: the api_key to be used for the new connection. If not specified the current value will be used
1807+
Creates a new client created with the given base_url and/api_key.
1808+
1809+
:param base_url: the base_url to be used for the new connection.
1810+
If not specified the current value will be used
1811+
:param api_key: the api_key to be used for the new connection.
1812+
If not specified the current value will be used
18601813
"""
1861-
current_client = AtlanClient.get_current_client()
18621814
tmp_client = AtlanClient(
1863-
base_url=base_url or current_client.base_url,
1864-
api_key=api_key or current_client.api_key,
1815+
base_url=base_url or client.base_url,
1816+
api_key=api_key or client.api_key,
18651817
connect_timeout=connect_timeout,
18661818
read_timeout=read_timeout,
18671819
retry=retry,
18681820
)
1869-
try:
1870-
yield tmp_client
1871-
finally:
1872-
AtlanClient.set_current_client(current_client)
1821+
yield tmp_client
18731822

18741823

18751824
from pyatlan.model.keycloak_events import ( # noqa: E402

pyatlan/client/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def _add_as(
414414
)
415415

416416
token_user = self.get_current().username or ""
417-
with client_connection(api_key=impersonation_token) as tmp:
417+
with client_connection(client=self._client, api_key=impersonation_token) as tmp: # type: ignore[arg-type]
418418
request = (
419419
FluentSearch()
420420
.where(Asset.GUID.eq(asset_guid))

pyatlan/model/assets/core/referenceable.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,6 @@ def can_be_archived(self) -> bool:
103103
"""
104104
return True
105105

106-
# @property
107-
# def atlan_tag_names(self) -> List[str]:
108-
# from pyatlan.client.atlan import AtlanClient
109-
# from pyatlan.model.constants import DELETED_
110-
111-
# if self.classification_names:
112-
# return [
113-
# AtlanClient.get_current_client().atlan_tag_cache.get_name_for_id(tag_id)
114-
# or DELETED_
115-
# for tag_id in self.classification_names
116-
# ]
117-
# return []
118106
@property
119107
def atlan_tag_names(self) -> List[str]:
120108
return self.classification_names or []

tests/integration/purpose_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,10 @@ def test_token_permissions(client: AtlanClient, token):
299299

300300
@pytest.mark.skip(reason="Test failing with HekaException")
301301
@pytest.mark.order(after="test_token_permissions")
302-
def test_run_query_with_policy(assign_tag_to_asset, token, query):
303-
with client_connection(api_key=token.attributes.access_token) as redacted:
302+
def test_run_query_with_policy(assign_tag_to_asset, token, query, client: AtlanClient):
303+
with client_connection(
304+
client=client, api_key=token.attributes.access_token
305+
) as redacted:
304306
# The policy will take some time to go into effect
305307
# start by waiting a reasonable set amount of time
306308
# (limit the same query re-running multiple times on data store)

tests/integration/test_index_search.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,9 @@ def test_purpose_search(client: AtlanClient, known_issues_purpose: Purpose):
843843

844844
def test_read_timeout(client: AtlanClient):
845845
request = (FluentSearch().select()).to_request()
846-
with client_connection(read_timeout=0.1, retry=Retry(total=0)) as timed_client:
846+
with client_connection(
847+
client=client, read_timeout=0.1, retry=Retry(total=0)
848+
) as timed_client:
847849
with pytest.raises(
848850
requests.exceptions.ReadTimeout,
849851
match=".Read timed out\. \(read timeout=0\.1\)", # noqa W605
@@ -854,7 +856,7 @@ def test_read_timeout(client: AtlanClient):
854856
def test_connect_timeout(client: AtlanClient):
855857
request = (FluentSearch().select()).to_request()
856858
with client_connection(
857-
connect_timeout=0.0001, retry=Retry(total=0)
859+
client=client, connect_timeout=0.0001, retry=Retry(total=0)
858860
) as timed_client:
859861
with pytest.raises(
860862
requests.exceptions.ConnectionError,

0 commit comments

Comments
 (0)