1313 Tuple ,
1414 Type ,
1515 Union ,
16+ get_origin ,
1617 get_type_hints ,
1718)
1819from warnings import warn
1920
2021from pydantic .alias_generators import to_pascal
22+ from typing_extensions import get_args
2123
2224from specklepy .logging .exceptions import SpeckleException
2325from specklepy .transports .memory import MemoryTransport
@@ -224,28 +226,27 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
224226 if isinstance (t , ForwardRef ):
225227 return True , value
226228
227- origin = t .__origin__
229+ origin = get_origin (t )
230+ args = get_args (t )
231+
228232 # below is what in nicer for >= py38
229233 # origin = get_origin(t)
230234
231235 # recursive validation for Unions on both types preferring the fist type
232- if origin is Union :
233- # below is what in nicer for >= py38
234- # t_1, t_2 = get_args(t)
235- args = t .__args__ # type: ignore
236- for arg_t in args :
237- t_success , t_value = _validate_type (arg_t , value )
238- if t_success :
239- return True , t_value
240- return False , value
236+ # if origin is Union or isinstance(t, UnionType):
237+ # for arg_t in args:
238+ # ok, v = _validate_type(arg_t, value)
239+ # if ok:
240+ # return True, v
241+ # return False, value
241242 if origin is dict :
242243 if not isinstance (value , dict ):
243244 return False , value
244- if value == {} :
245+ if not value :
245246 return True , value
246- if not getattr ( t , "__args__" , None ) :
247+ if not args :
247248 return True , value
248- t_key , t_value = t . __args__ # type: ignore
249+ t_key , t_value = args
249250
250251 if (
251252 getattr (t_key , "__name__" , None ),
@@ -265,11 +266,11 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
265266 if origin is list :
266267 if not isinstance (value , list ):
267268 return False , value
268- if value == [] :
269+ if not value :
269270 return True , value
270- if not hasattr ( t , "__args__" ) :
271+ if not args :
271272 return True , value
272- t_items = t . __args__ [0 ] # type: ignore
273+ t_items = args [0 ]
273274 if getattr (t_items , "__name__" , None ) == "T" :
274275 return True , value
275276 first_item_valid , _ = _validate_type (t_items , value [0 ])
@@ -280,10 +281,10 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
280281 if origin is tuple :
281282 if not isinstance (value , tuple ):
282283 return False , value
283- if not hasattr ( t , "__args__" ) :
284+ if not args :
284285 return True , value
285286 args = t .__args__ # type: ignore
286- if args == tuple () :
287+ if not args :
287288 return True , value
288289 # we're not checking for empty tuple, cause tuple lengths must match
289290 if len (args ) != len (value ):
@@ -299,7 +300,7 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
299300 if origin is set :
300301 if not isinstance (value , set ):
301302 return False , value
302- if not hasattr ( t , "__args__" ) :
303+ if not args :
303304 return True , value
304305 t_items = t .__args__ [0 ] # type: ignore
305306 first_item_valid , _ = _validate_type (t_items , next (iter (value )))
0 commit comments