11from abc import ABCMeta , abstractmethod
2- from functools import wraps
3- from typing import Callable , Iterable , Set , Union , Any
2+ from functools import lru_cache , wraps
3+ from typing import Callable , FrozenSet , Iterable , Set , Union , Any
44
55from slack_sdk .errors import SlackObjectFormationError
66
@@ -21,6 +21,15 @@ def __str__(self):
2121]
2222
2323
24+ def _is_validator (value : Any ) -> bool :
25+ return callable (value ) and hasattr (value , "validator" )
26+
27+
28+ @lru_cache (maxsize = None )
29+ def _class_validator_names (cls : type ) -> FrozenSet [str ]:
30+ return frozenset (name for name in dir (cls ) if not name .startswith ("__" ) and _is_validator (getattr (cls , name , None )))
31+
32+
2433class JsonObject (BaseObject , metaclass = ABCMeta ):
2534 """The base class for JSON serializable class objects."""
2635
@@ -36,10 +45,12 @@ def validate_json(self) -> None:
3645 Raises:
3746 SlackObjectFormationError: if the object was not valid
3847 """
39- for attribute in (func for func in dir (self ) if not func .startswith ("__" )):
40- method = getattr (self , attribute , None )
41- if callable (method ) and hasattr (method , "validator" ):
42- method ()
48+ instance_attribute_names = (name for name in vars (self ) if not name .startswith ("__" ))
49+ candidate_names = _class_validator_names (type (self )).union (instance_attribute_names )
50+ for name in sorted (candidate_names ):
51+ attribute : Any = getattr (self , name , None )
52+ if _is_validator (attribute ):
53+ attribute ()
4354
4455 def get_object_attribute (self , key : str ):
4556 return getattr (self , key , None )
0 commit comments