@@ -1031,35 +1031,25 @@ def from_api_repr(cls, resource: dict):
10311031 return config
10321032
10331033
1034- def _isinstance_or_raise (value : Any , dtype : Optional [ Union [ Any , Tuple [ Any , ...]] ]):
1034+ def _isinstance_or_raise (value : Any , dtype : type , none_allowed : Optional [ bool ]):
10351035 """Determine whether a value type matches a given datatype or None.
10361036
10371037 Args:
10381038 value (Any): Value to be checked.
1039- dtype (Optional[Union[Any, Tuple[Any, ...]]]): Expected data type(s).
1039+ dtype (type): Expected data type(s).
1040+ none_allowed Optional(bool): whether value is allowed to be None.
10401041
10411042 Returns:
10421043 Any: Returns the input value if the type check is successful.
10431044
10441045 Raises:
10451046 TypeError: If the input value's type does not match the expected data type(s).
10461047 """
1047-
1048- # Simplest case
1049- if dtype is None and value is None :
1048+ if none_allowed and value is None :
1049+ return value
1050+
1051+ if isinstance (value , dtype ):
10501052 return value
10511053
1052- elif isinstance (dtype , tuple ):
1053- # Another simple case
1054- if None in dtype and value is None :
1055- return value
1056- # Iterate through the tuple and check if value is an instance of any type
1057- if not any (isinstance (value , t ) for t in dtype if t is not None ):
1058- valid_types_str = ", " .join (str (t ) for t in dtype if t is not None )
1059- msg = f"Pass { value } as one of '{ valid_types_str } ' or None. Got { type (value )} ."
1060- raise TypeError (msg )
1061- else :
1062- if not isinstance (value , dtype ):
1063- msg = f"Pass { value } as a '{ dtype } '. Got { type (value )} ."
1064- raise TypeError (msg )
1065- return value
1054+ msg = f"Pass { value } as a '{ dtype } ' (or None). Got { type (value )} ." # Add the 'or None' conditionally
1055+ raise TypeError (msg )
0 commit comments