@@ -803,8 +803,10 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
803
803
fallback = info .metaclass_type or builtin_type ('builtins.type' )
804
804
if init_index < new_index :
805
805
method = init_method .node # type: Union[FuncBase, Decorator]
806
+ is_new = False
806
807
elif init_index > new_index :
807
808
method = new_method .node
809
+ is_new = True
808
810
else :
809
811
if init_method .node .info .fullname () == 'builtins.object' :
810
812
# Both are defined by object. But if we've got a bogus
@@ -823,14 +825,15 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
823
825
# is the right thing, but __new__ caused problems with
824
826
# typeshed (#5647).
825
827
method = init_method .node
828
+ is_new = False
826
829
# Construct callable type based on signature of __init__. Adjust
827
830
# return type and insert type arguments.
828
831
if isinstance (method , FuncBase ):
829
832
t = function_type (method , fallback )
830
833
else :
831
834
assert isinstance (method .type , FunctionLike ) # is_valid_constructor() ensures this
832
835
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 )
834
837
835
838
836
839
def is_valid_constructor (n : Optional [SymbolNode ]) -> bool :
@@ -849,7 +852,8 @@ def is_valid_constructor(n: Optional[SymbolNode]) -> bool:
849
852
def type_object_type_from_function (signature : FunctionLike ,
850
853
info : TypeInfo ,
851
854
def_info : TypeInfo ,
852
- fallback : Instance ) -> FunctionLike :
855
+ fallback : Instance ,
856
+ is_new : bool ) -> FunctionLike :
853
857
# The __init__ method might come from a generic superclass
854
858
# (init_or_new.info) with type variables that do not map
855
859
# identically to the type variables of the class being constructed
@@ -859,7 +863,7 @@ def type_object_type_from_function(signature: FunctionLike,
859
863
# class B(A[List[T]], Generic[T]): pass
860
864
#
861
865
# 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 )
863
867
signature = cast (FunctionLike ,
864
868
map_type_from_supertype (signature , info , def_info ))
865
869
special_sig = None # type: Optional[str]
@@ -868,25 +872,32 @@ def type_object_type_from_function(signature: FunctionLike,
868
872
special_sig = 'dict'
869
873
870
874
if isinstance (signature , CallableType ):
871
- return class_callable (signature , info , fallback , special_sig )
875
+ return class_callable (signature , info , fallback , special_sig , is_new )
872
876
else :
873
877
# Overloaded __init__/__new__.
874
878
assert isinstance (signature , Overloaded )
875
879
items = [] # type: List[CallableType]
876
880
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 ))
878
882
return Overloaded (items )
879
883
880
884
881
885
def 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 = False ) -> CallableType :
883
888
"""Create a type object type based on the signature of __init__."""
884
889
variables = [] # type: List[TypeVarDef]
885
890
variables .extend (info .defn .type_vars )
886
891
variables .extend (init_type .variables )
887
892
893
+ is_new = True
894
+ if is_new and isinstance (init_type .ret_type , (Instance , TupleType )):
895
+ ret_type = init_type .ret_type # type: Type
896
+ else :
897
+ ret_type = fill_typevars (info )
898
+
888
899
callable_type = init_type .copy_modified (
889
- ret_type = fill_typevars ( info ) , fallback = type_type , name = None , variables = variables ,
900
+ ret_type = ret_type , fallback = type_type , name = None , variables = variables ,
890
901
special_sig = special_sig )
891
902
c = callable_type .with_name (info .name ())
892
903
return c
0 commit comments