Skip to content

Commit f040a7f

Browse files
committed
[chores + tests] Removed commented code and refactored tests
1 parent 1a4119e commit f040a7f

File tree

6 files changed

+188
-176
lines changed

6 files changed

+188
-176
lines changed

pyatlan/client/atlan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ def _create_params(
646646
params["params"] = query_params
647647
if request_obj is not None:
648648
if isinstance(request_obj, AtlanObject):
649-
params["data"] = AtlanRequest(request_obj, client=self).json()
649+
params["data"] = AtlanRequest(instance=request_obj, client=self).json()
650650
elif api.consumes == APPLICATION_ENCODED_FORM:
651651
params["data"] = request_obj
652652
else:

pyatlan/model/core.py

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,7 @@ def __new__(cls, *args, **kwargs):
4343
return obj
4444

4545
def __init__(self, display_text: str):
46-
# from pyatlan.client.atlan import AtlanClient
47-
48-
# if not (
49-
# id := AtlanClient.get_current_client().atlan_tag_cache.get_id_for_name(
50-
# display_text
51-
# )
52-
# ):
53-
# raise ValueError(f"{display_text} is not a valid Classification")
5446
self._display_text = display_text
55-
# self._id = id
56-
57-
# @property
58-
# def id(self):
59-
# return self._id
6047

6148
@classmethod
6249
def get_deleted_sentinel(cls) -> "AtlanTagName":
@@ -84,35 +71,12 @@ def __eq__(self, other):
8471
and self._display_text == other._display_text
8572
)
8673

87-
# @classmethod
88-
# def _convert_to_display_text(cls, data):
89-
# from pyatlan.client.atlan import AtlanClient
90-
91-
# if isinstance(data, AtlanTagName):
92-
# return data
93-
94-
# if (
95-
# display_text
96-
# := AtlanClient.get_current_client().atlan_tag_cache.get_name_for_id(data)
97-
# ):
98-
# return AtlanTagName(display_text)
99-
# else:
100-
# return cls.get_deleted_sentinel()
101-
10274
@classmethod
10375
def _convert_to_tag_name(cls, data):
10476
if isinstance(data, AtlanTagName):
10577
return data
10678
return AtlanTagName(data) if data else cls.get_deleted_sentinel()
10779

108-
# @staticmethod
109-
# def json_encode_atlan_tag(atlan_tag_name: "AtlanTagName"):
110-
# from pyatlan.client.atlan import AtlanClient
111-
112-
# return AtlanClient.get_current_client().atlan_tag_cache.get_id_for_name(
113-
# atlan_tag_name._display_text
114-
# )
115-
11680

11781
class AtlanObject(BaseModel):
11882
__atlan_extra__: Dict[str, Any] = Field(
@@ -222,20 +186,20 @@ def to_dict(self) -> Union[Dict[str, Any], List[Any], Any]:
222186

223187

224188
class AtlanRequest:
225-
def __init__(self, model: AtlanObject, client: AtlanClient):
189+
def __init__(self, instance: AtlanObject, client: AtlanClient):
226190
self.client = client
227-
self.model = model
191+
self.instance = instance
228192
self.retranslators = [
229193
AtlanTagRetranslator(client),
230194
# add others...
231195
]
232-
# Do: model.json() → parse → translate → store
196+
# Do: instance.json() → parse → translate → store
233197
try:
234-
raw_json = self.model.json(
198+
raw_json = self.instance.json(
235199
by_alias=True, exclude_unset=True, client=self.client
236200
)
237201
except TypeError:
238-
raw_json = self.model.json(
202+
raw_json = self.instance.json(
239203
by_alias=True,
240204
exclude_unset=True,
241205
)
@@ -328,7 +292,7 @@ class Config:
328292
)
329293

330294
attributes: Optional[Dict[str, Any]] = None
331-
tag_id: Optional[str] = Field(default=True, exclude=True)
295+
tag_id: Optional[str] = Field(default=None, exclude=True)
332296

333297
# @property
334298
# def source_tag_attachements(self) -> List[SourceTagAttachment]:

pyatlan/model/retranslators.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def retranslate(self, data: Dict[str, Any]) -> Dict[str, Any]:
1919

2020
class AtlanTagRetranslator(BaseRetranslator):
2121
_TYPE_NAME = "typeName"
22-
_CLASSIFICATION_NAMES = "classificationNames"
2322
_SOURCE_ATTACHMENTS = "source_tag_attachements"
23+
_CLASSIFICATION_NAMES = {"classificationNames", "purposeClassifications"}
2424
_CLASSIFICATION_KEYS = {
2525
"classifications",
2626
"addOrUpdateClassifications",
@@ -31,34 +31,35 @@ def __init__(self, client: "AtlanClient"):
3131
self.client = client
3232

3333
def applies_to(self, data: Dict[str, Any]) -> bool:
34-
return self._CLASSIFICATION_NAMES in data or any(
34+
return any(key in data for key in self._CLASSIFICATION_NAMES) or any(
3535
key in data for key in self._CLASSIFICATION_KEYS
3636
)
3737

3838
def retranslate(self, data: Dict[str, Any]) -> Dict[str, Any]:
39+
from pyatlan.model.constants import DELETED_
40+
3941
data = data.copy()
4042

41-
# Convert classification names → IDs
42-
if self._CLASSIFICATION_NAMES in data:
43-
data[self._CLASSIFICATION_NAMES] = [
44-
self.client.atlan_tag_cache.get_id_for_name(str(name))
45-
for name in data[self._CLASSIFICATION_NAMES]
46-
]
43+
# Convert classification human-readable name → hash ID
44+
for key in self._CLASSIFICATION_NAMES:
45+
if key in data:
46+
data[key] = [
47+
self.client.atlan_tag_cache.get_id_for_name(str(name)) or DELETED_
48+
for name in data[key]
49+
]
4750

48-
# Convert classification objects
51+
# Convert classification objects human-readable name typeName → hash ID
4952
for key in self._CLASSIFICATION_KEYS:
5053
if key in data:
5154
for classification in data[key]:
5255
tag_name = str(classification.get(self._TYPE_NAME))
5356
if tag_name:
5457
tag_id = self.client.atlan_tag_cache.get_id_for_name(tag_name)
55-
if not tag_id:
56-
continue
57-
classification[self._TYPE_NAME] = tag_id
58+
classification[self._TYPE_NAME] = tag_id if tag_id else DELETED_
5859

5960
# Rebuild source tag attributes
6061
attachments = classification.pop(self._SOURCE_ATTACHMENTS, None)
61-
if attachments:
62+
if attachments and tag_id:
6263
attr_id = (
6364
self.client.atlan_tag_cache.get_source_tags_attr_id(
6465
tag_id
@@ -72,5 +73,4 @@ def retranslate(self, data: Dict[str, Any]) -> Dict[str, Any]:
7273
}
7374
for attachment in attachments
7475
]
75-
7676
return data

pyatlan/model/translators.py

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,60 @@ def translate(self, data: Dict[str, Any]) -> Dict[str, Any]:
2020

2121

2222
class AtlanTagTranslator(BaseTranslator):
23+
_TAG_ID = "tag_id"
2324
_TYPE_NAME = "typeName"
24-
_CLASSIFICATIONS = "classifications"
25-
_CLASSIFICATION_NAMES = "classificationNames"
25+
_SOURCE_ATTACHMENTS = "source_tag_attachements"
26+
_CLASSIFICATION_NAMES = {"classificationNames", "purposeClassifications"}
27+
_CLASSIFICATION_KEYS = {
28+
"classifications",
29+
"addOrUpdateClassifications",
30+
"removeClassifications",
31+
}
2632

2733
def __init__(self, client: AtlanClient):
2834
self.client = client
2935

3036
def applies_to(self, data: Dict[str, Any]) -> bool:
31-
return self._CLASSIFICATION_NAMES in data or self._CLASSIFICATIONS in data
37+
return any(key in data for key in self._CLASSIFICATION_NAMES) or any(
38+
key in data for key in self._CLASSIFICATION_KEYS
39+
)
3240

3341
def translate(self, data: Dict[str, Any]) -> Dict[str, Any]:
42+
from pyatlan.model.constants import DELETED_
43+
3444
raw_json = data.copy()
3545

36-
if self._CLASSIFICATION_NAMES in raw_json:
37-
raw_json[self._CLASSIFICATION_NAMES] = [
38-
self.client.atlan_tag_cache.get_name_for_id(tag_id)
39-
for tag_id in raw_json[self._CLASSIFICATION_NAMES]
40-
]
46+
# Convert classification hash ID → human-readable name
47+
for key in self._CLASSIFICATION_NAMES:
48+
if key in raw_json:
49+
raw_json[key] = [
50+
self.client.atlan_tag_cache.get_name_for_id(tag_id) or DELETED_
51+
for tag_id in raw_json[key]
52+
]
4153

42-
if self._CLASSIFICATIONS in raw_json:
43-
for classification in raw_json[self._CLASSIFICATIONS]:
44-
tag_id = classification.get(self._TYPE_NAME)
45-
if tag_id:
46-
tag_name = self.client.atlan_tag_cache.get_name_for_id(tag_id)
47-
if not tag_name:
48-
continue
49-
classification[self._TYPE_NAME] = tag_name
50-
classification["tag_id"] = tag_id
51-
# Check if the tag is a source tag (in that case tag has "attributes")
52-
attr_id = self.client.atlan_tag_cache.get_source_tags_attr_id(
53-
tag_id
54-
)
55-
if attr_id:
56-
classification["source_tag_attachements"] = [
57-
SourceTagAttachment(**source_tag["attributes"])
58-
for source_tag in classification.get("attributes").get(
59-
attr_id
60-
)
61-
if isinstance(source_tag, dict)
62-
and source_tag.get("attributes")
63-
]
54+
# Convert classification objects typeName hash ID → human-readable name
55+
for key in self._CLASSIFICATION_KEYS:
56+
if key in raw_json:
57+
for classification in raw_json[key]:
58+
tag_id = classification.get(self._TYPE_NAME)
59+
if tag_id:
60+
tag_name = self.client.atlan_tag_cache.get_name_for_id(tag_id)
61+
classification[self._TYPE_NAME] = (
62+
tag_name if tag_name else DELETED_
63+
)
64+
classification[self._TAG_ID] = tag_id
65+
# Check if the tag is a source tag (in that case tag has "attributes")
66+
attr_id = self.client.atlan_tag_cache.get_source_tags_attr_id(
67+
tag_id
68+
)
69+
if attr_id:
70+
classification[self._SOURCE_ATTACHMENTS] = [
71+
SourceTagAttachment(**source_tag["attributes"])
72+
for source_tag in classification.get("attributes").get(
73+
attr_id
74+
)
75+
if isinstance(source_tag, dict)
76+
and source_tag.get("attributes")
77+
]
6478

6579
return raw_json

0 commit comments

Comments
 (0)