@@ -402,23 +402,32 @@ def visit_instance(self, t: Instance) -> ProperType:
402402 return self .default (self .s )
403403
404404 def visit_callable_type (self , t : CallableType ) -> ProperType :
405- if isinstance (self .s , CallableType ) and is_similar_callables (t , self .s ):
406- if is_equivalent (t , self .s ):
407- return combine_similar_callables (t , self .s )
408- result = join_similar_callables (t , self .s )
405+ if isinstance (self .s , CallableType ):
406+ if is_similar_callables (t , self .s ):
407+ if is_equivalent (t , self .s ):
408+ return combine_similar_callables (t , self .s )
409+ result = join_similar_callables (t , self .s )
410+ if any (
411+ isinstance (tp , (NoneType , UninhabitedType ))
412+ for tp in get_proper_types (result .arg_types )
413+ ):
414+ # We don't want to return unusable Callable, attempt fallback instead.
415+ return join_types (t .fallback , self .s )
416+ else :
417+ if is_subtype (self .s , t ):
418+ result = t .copy_modified ()
419+ elif is_subtype (t , self .s ):
420+ result = self .s .copy_modified ()
421+ else :
422+ return join_types (t .fallback , self .s )
423+
409424 # We set the from_type_type flag to suppress error when a collection of
410425 # concrete class objects gets inferred as their common abstract superclass.
411426 if not (
412427 (t .is_type_obj () and t .type_object ().is_abstract )
413428 or (self .s .is_type_obj () and self .s .type_object ().is_abstract )
414429 ):
415430 result .from_type_type = True
416- if any (
417- isinstance (tp , (NoneType , UninhabitedType ))
418- for tp in get_proper_types (result .arg_types )
419- ):
420- # We don't want to return unusable Callable, attempt fallback instead.
421- return join_types (t .fallback , self .s )
422431 return result
423432 elif isinstance (self .s , Overloaded ):
424433 # Switch the order of arguments to that we'll get to visit_overloaded.
0 commit comments