11from 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
65class 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
6160class 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
737740class 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
12051169class EventAndEntityMetadata (JsonObject ):
12061170 """Message metadata with entities
0 commit comments