@@ -32,7 +32,9 @@ def __init__(
3232 self ,
3333 desired_to_actual_to_wrapper : Optional [Dict [
3434 Type [T_DESIRED ],
35- Dict [Type [T_ACTUAL ], Callable [[T_ACTUAL ], T_DESIRED ]]]]= None
35+ Dict [Type [T_ACTUAL ],
36+ Callable [[T_ACTUAL ],
37+ Optional [T_DESIRED ]]]]]= None
3638 ) -> None :
3739 """Specifies extensions.
3840
@@ -48,12 +50,12 @@ def __init__(
4850 {}
4951 if desired_to_actual_to_wrapper is None
5052 else desired_to_actual_to_wrapper
51- ) # type: Dict[Type[Any], Dict[Any, Callable[[Any], Any]]]
53+ ) # type: Dict[Type[Any], Dict[Any, Callable[[Any], Optional[ Any] ]]]
5254
5355 def add_cast (self ,
5456 desired_type : Type [T_DESIRED ],
5557 actual_type : Type [T_ACTUAL ],
56- conversion : Callable [[T_ACTUAL ], T_DESIRED ],
58+ conversion : Callable [[T_ACTUAL ], Optional [ T_DESIRED ] ],
5759 also_add_inherited_conversions : bool = True ,
5860 overwrite_existing : bool = False ) -> None :
5961 """Adds a way to turn one type of thing into another.
@@ -139,7 +141,9 @@ def try_cast(self,
139141 for actual_type in inspect .getmro (type (actual_value )):
140142 wrapper = actual_to_wrapper .get (actual_type )
141143 if wrapper :
142- return wrapper (actual_value )
144+ wrapped = wrapper (actual_value )
145+ if wrapped is not None :
146+ return wrapped
143147
144148 if isinstance (actual_value , desired_type ):
145149 return actual_value
0 commit comments