@@ -803,8 +803,10 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
803803 fallback = info .metaclass_type or builtin_type ('builtins.type' )
804804 if init_index < new_index :
805805 method = init_method .node # type: Union[FuncBase, Decorator]
806+ is_new = False
806807 elif init_index > new_index :
807808 method = new_method .node
809+ is_new = True
808810 else :
809811 if init_method .node .info .fullname () == 'builtins.object' :
810812 # Both are defined by object. But if we've got a bogus
@@ -817,20 +819,21 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
817819 arg_names = ["_args" , "_kwds" ],
818820 ret_type = any_type ,
819821 fallback = builtin_type ('builtins.function' ))
820- return class_callable (sig , info , fallback , None )
822+ return class_callable (sig , info , fallback , None , is_new = False )
821823
822824 # Otherwise prefer __init__ in a tie. It isn't clear that this
823825 # is the right thing, but __new__ caused problems with
824826 # typeshed (#5647).
825827 method = init_method .node
828+ is_new = False
826829 # Construct callable type based on signature of __init__. Adjust
827830 # return type and insert type arguments.
828831 if isinstance (method , FuncBase ):
829832 t = function_type (method , fallback )
830833 else :
831834 assert isinstance (method .type , FunctionLike ) # is_valid_constructor() ensures this
832835 t = method .type
833- return type_object_type_from_function (t , info , method .info , fallback )
836+ return type_object_type_from_function (t , info , method .info , fallback , is_new )
834837
835838
836839def is_valid_constructor (n : Optional [SymbolNode ]) -> bool :
@@ -849,7 +852,8 @@ def is_valid_constructor(n: Optional[SymbolNode]) -> bool:
849852def type_object_type_from_function (signature : FunctionLike ,
850853 info : TypeInfo ,
851854 def_info : TypeInfo ,
852- fallback : Instance ) -> FunctionLike :
855+ fallback : Instance ,
856+ is_new : bool ) -> FunctionLike :
853857 # The __init__ method might come from a generic superclass
854858 # (init_or_new.info) with type variables that do not map
855859 # identically to the type variables of the class being constructed
@@ -859,7 +863,7 @@ def type_object_type_from_function(signature: FunctionLike,
859863 # class B(A[List[T]], Generic[T]): pass
860864 #
861865 # We need to first map B's __init__ to the type (List[T]) -> None.
862- signature = bind_self (signature )
866+ signature = bind_self (signature , original_type = fill_typevars ( info ), is_classmethod = is_new )
863867 signature = cast (FunctionLike ,
864868 map_type_from_supertype (signature , info , def_info ))
865869 special_sig = None # type: Optional[str]
@@ -868,25 +872,31 @@ def type_object_type_from_function(signature: FunctionLike,
868872 special_sig = 'dict'
869873
870874 if isinstance (signature , CallableType ):
871- return class_callable (signature , info , fallback , special_sig )
875+ return class_callable (signature , info , fallback , special_sig , is_new )
872876 else :
873877 # Overloaded __init__/__new__.
874878 assert isinstance (signature , Overloaded )
875879 items = [] # type: List[CallableType]
876880 for item in signature .items ():
877- items .append (class_callable (item , info , fallback , special_sig ))
881+ items .append (class_callable (item , info , fallback , special_sig , is_new ))
878882 return Overloaded (items )
879883
880884
881885def class_callable (init_type : CallableType , info : TypeInfo , type_type : Instance ,
882- special_sig : Optional [str ]) -> CallableType :
886+ special_sig : Optional [str ],
887+ is_new : bool ) -> CallableType :
883888 """Create a type object type based on the signature of __init__."""
884889 variables = [] # type: List[TypeVarDef]
885890 variables .extend (info .defn .type_vars )
886891 variables .extend (init_type .variables )
887892
893+ if is_new and isinstance (init_type .ret_type , (Instance , TupleType )):
894+ ret_type = init_type .ret_type # type: Type
895+ else :
896+ ret_type = fill_typevars (info )
897+
888898 callable_type = init_type .copy_modified (
889- ret_type = fill_typevars ( info ) , fallback = type_type , name = None , variables = variables ,
899+ ret_type = ret_type , fallback = type_type , name = None , variables = variables ,
890900 special_sig = special_sig )
891901 c = callable_type .with_name (info .name ())
892902 return c
0 commit comments