Skip to content

Commit 7b48524

Browse files
committed
address feedback
1 parent 0be9838 commit 7b48524

2 files changed

Lines changed: 43 additions & 74 deletions

File tree

‎slack_sdk/models/basic_objects.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def validate_json(self) -> None:
4040
if callable(method) and hasattr(method, "validator"):
4141
method()
4242

43+
def get_attribute(self, key: str):
44+
return getattr(self, key, None)
45+
4346
def get_non_null_attributes(self) -> dict:
4447
"""
4548
Construct a dictionary out of non-null keys (from attributes property)
@@ -57,7 +60,7 @@ def to_dict_compatible(value: Union[dict, list, object, tuple]) -> Union[dict, l
5760
return value
5861

5962
def is_not_empty(self, key: str) -> bool:
60-
value = getattr(self, key, None)
63+
value = self.get_attribute(self, key)
6164
if value is None:
6265
return False
6366

@@ -75,7 +78,9 @@ def is_not_empty(self, key: str) -> bool:
7578
return value is not None
7679

7780
return {
78-
key: to_dict_compatible(getattr(self, key, None)) for key in sorted(self.attributes) if is_not_empty(self, key)
81+
key: to_dict_compatible(value=self.get_attribute(self, key))
82+
for key in sorted(self.attributes)
83+
if is_not_empty(self, key)
7984
}
8085

8186
def to_dict(self, *args) -> dict:

‎slack_sdk/models/metadata/__init__.py‎

Lines changed: 36 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Dict, Any, Union, Optional, List
2-
from enum import StrEnum
3-
from slack_sdk.models.basic_objects import JsonObject
2+
from slack_sdk.models.basic_objects import JsonObject, EnumValidator, EMPTY_ALLOWED_TYPE_AND_PROPERTY_LIST
43

54

65
class Metadata(JsonObject):
@@ -34,28 +33,28 @@ def __repr__(self):
3433
## Work object entity metadata
3534

3635

37-
class EntityType(StrEnum):
38-
"""Entity type enumeration"""
36+
"""Entity types"""
37+
EntityType = {
38+
"slack#/entities/task",
39+
"slack#/entities/file",
40+
"slack#/entities/item",
41+
"slack#/entities/incident",
42+
"slack#/entities/content_item",
43+
}
3944

40-
TASK = "slack#/entities/task"
41-
FILE = "slack#/entities/file"
42-
ITEM = "slack#/entities/item"
43-
INCIDENT = "slack#/entities/incident"
44-
CONTENT_ITEM = "slack#/entities/content_item"
4545

46-
47-
class CustomFieldType(StrEnum):
48-
"""Custom field type enumeration"""
49-
50-
INTEGER = "integer"
51-
STRING = "string"
52-
ARRAY = "array"
53-
DATE = "slack#/types/date"
54-
TIMESTAMP = "slack#/types/timestamp"
55-
IMAGE = "slack#/types/image"
56-
CHANNEL_ID = "slack#/types/channel_id"
57-
USER = "slack#/types/user"
58-
ENTITY_REF = "slack#/types/entity_ref"
46+
"""Custom field types"""
47+
CustomFieldType = {
48+
"integer",
49+
"string",
50+
"array",
51+
"slack#/types/date",
52+
"slack#/types/timestamp",
53+
"slack#/types/image",
54+
"slack#/types/channel_id",
55+
"slack#/types/user",
56+
"slack#/types/entity_ref",
57+
}
5958

6059

6160
class ExternalRef(JsonObject):
@@ -693,7 +692,7 @@ def __init__(
693692
self,
694693
label: str,
695694
key: str,
696-
type: Union[str, CustomFieldType],
695+
type: str,
697696
value: Optional[Union[str, int, List[Union[Dict[str, Any], EntityArrayItemField]]]] = None,
698697
link: Optional[str] = None,
699698
icon: Optional[Union[Dict[str, Any], EntityIconField]] = None,
@@ -733,6 +732,10 @@ def __str__(self):
733732
def __repr__(self):
734733
return self.__str__()
735734

735+
@EnumValidator("type", CustomFieldType)
736+
def type_valid(self):
737+
return self.type is None or self.type in CustomFieldType
738+
736739

737740
class FileEntityFields(JsonObject):
738741
"""Fields specific to file entities"""
@@ -1109,54 +1112,11 @@ def entity_attributes(self, value: Union[Dict[str, Any], EntityAttributes]):
11091112
"""Set the entity attributes data."""
11101113
self._entity_attributes = value
11111114

1112-
def get_non_null_attributes(self) -> dict:
1113-
"""Override to handle the 'attributes' field name conflict"""
1114-
from slack_sdk.models.basic_objects import EMPTY_ALLOWED_TYPE_AND_PROPERTY_LIST
1115-
1116-
def to_dict_compatible(value):
1117-
if isinstance(value, (list, tuple)):
1118-
return [to_dict_compatible(v) for v in value]
1119-
else:
1120-
to_dict = getattr(value, "to_dict", None)
1121-
if to_dict and callable(to_dict):
1122-
return {k: to_dict_compatible(v) for k, v in value.to_dict().items()}
1123-
else:
1124-
return value
1125-
1126-
def is_not_empty(key: str) -> bool:
1127-
# Special handling for 'attributes' key - map to internal _entity_attributes
1128-
if key == "attributes":
1129-
value = self._entity_attributes
1130-
else:
1131-
value = getattr(self, key, None)
1132-
1133-
if value is None:
1134-
return False
1135-
1136-
# Handle empty array exceptions (from base class logic)
1137-
type_value = getattr(self, "type", None)
1138-
for empty_allowed in EMPTY_ALLOWED_TYPE_AND_PROPERTY_LIST:
1139-
if type_value == empty_allowed["type"] and key == empty_allowed["property"]:
1140-
return True
1141-
1142-
has_len = getattr(value, "__len__", None) is not None
1143-
if has_len:
1144-
return len(value) > 0
1145-
else:
1146-
return value is not None
1147-
1148-
result = {}
1149-
# Use the class-level attributes set
1150-
for key in sorted(type(self).attributes):
1151-
if is_not_empty(key):
1152-
# Special handling for 'attributes' key - map to internal _entity_attributes
1153-
if key == "attributes":
1154-
value = self._entity_attributes
1155-
else:
1156-
value = getattr(self, key, None)
1157-
result[key] = to_dict_compatible(value)
1158-
1159-
return result
1115+
def get_attribute(self, key: str):
1116+
if key == "attributes":
1117+
return self._entity_attributes
1118+
else:
1119+
return getattr(self, key, None)
11601120

11611121
def __str__(self):
11621122
return str(self.get_non_null_attributes())
@@ -1181,7 +1141,7 @@ class EntityMetadata(JsonObject):
11811141

11821142
def __init__(
11831143
self,
1184-
entity_type: Union[str, EntityType],
1144+
entity_type: str,
11851145
entity_payload: Union[Dict[str, Any], EntityPayload],
11861146
external_ref: Union[Dict[str, Any], ExternalRef],
11871147
url: str,
@@ -1201,6 +1161,10 @@ def __str__(self):
12011161
def __repr__(self):
12021162
return self.__str__()
12031163

1164+
@EnumValidator("entity_type", EntityType)
1165+
def entity_type_valid(self):
1166+
return self.entity_type is None or self.entity_type in EntityType
1167+
12041168

12051169
class EventAndEntityMetadata(JsonObject):
12061170
"""Message metadata with entities

0 commit comments

Comments
 (0)