@@ -787,8 +787,10 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
787
787
fallback = info .metaclass_type or builtin_type ('builtins.type' )
788
788
if init_index < new_index :
789
789
method = init_method .node # type: Union[FuncBase, Decorator]
790
+ is_new = False
790
791
elif init_index > new_index :
791
792
method = new_method .node
793
+ is_new = True
792
794
else :
793
795
if init_method .node .info .fullname () == 'builtins.object' :
794
796
# Both are defined by object. But if we've got a bogus
@@ -807,14 +809,15 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
807
809
# is the right thing, but __new__ caused problems with
808
810
# typeshed (#5647).
809
811
method = init_method .node
812
+ is_new = False
810
813
# Construct callable type based on signature of __init__. Adjust
811
814
# return type and insert type arguments.
812
815
if isinstance (method , FuncBase ):
813
816
t = function_type (method , fallback )
814
817
else :
815
818
assert isinstance (method .type , FunctionLike ) # is_valid_constructor() ensures this
816
819
t = method .type
817
- return type_object_type_from_function (t , info , method .info , fallback )
820
+ return type_object_type_from_function (t , info , method .info , fallback , is_new )
818
821
819
822
820
823
def is_valid_constructor (n : Optional [SymbolNode ]) -> bool :
@@ -833,7 +836,8 @@ def is_valid_constructor(n: Optional[SymbolNode]) -> bool:
833
836
def type_object_type_from_function (signature : FunctionLike ,
834
837
info : TypeInfo ,
835
838
def_info : TypeInfo ,
836
- fallback : Instance ) -> FunctionLike :
839
+ fallback : Instance ,
840
+ is_new : bool ) -> FunctionLike :
837
841
# The __init__ method might come from a generic superclass
838
842
# (init_or_new.info) with type variables that do not map
839
843
# identically to the type variables of the class being constructed
@@ -843,7 +847,7 @@ def type_object_type_from_function(signature: FunctionLike,
843
847
# class B(A[List[T]], Generic[T]): pass
844
848
#
845
849
# We need to first map B's __init__ to the type (List[T]) -> None.
846
- signature = bind_self (signature )
850
+ signature = bind_self (signature , original_type = fill_typevars ( info ), is_classmethod = is_new )
847
851
signature = cast (FunctionLike ,
848
852
map_type_from_supertype (signature , info , def_info ))
849
853
special_sig = None # type: Optional[str]
@@ -852,25 +856,32 @@ def type_object_type_from_function(signature: FunctionLike,
852
856
special_sig = 'dict'
853
857
854
858
if isinstance (signature , CallableType ):
855
- return class_callable (signature , info , fallback , special_sig )
859
+ return class_callable (signature , info , fallback , special_sig , is_new )
856
860
else :
857
861
# Overloaded __init__/__new__.
858
862
assert isinstance (signature , Overloaded )
859
863
items = [] # type: List[CallableType]
860
864
for item in signature .items ():
861
- items .append (class_callable (item , info , fallback , special_sig ))
865
+ items .append (class_callable (item , info , fallback , special_sig , is_new ))
862
866
return Overloaded (items )
863
867
864
868
865
869
def class_callable (init_type : CallableType , info : TypeInfo , type_type : Instance ,
866
- special_sig : Optional [str ]) -> CallableType :
870
+ special_sig : Optional [str ],
871
+ is_new : bool = False ) -> CallableType :
867
872
"""Create a type object type based on the signature of __init__."""
868
873
variables = [] # type: List[TypeVarDef]
869
874
variables .extend (info .defn .type_vars )
870
875
variables .extend (init_type .variables )
871
876
877
+ is_new = True
878
+ if is_new and isinstance (init_type .ret_type , (Instance , TupleType )):
879
+ ret_type = init_type .ret_type # type: Type
880
+ else :
881
+ ret_type = fill_typevars (info )
882
+
872
883
callable_type = init_type .copy_modified (
873
- ret_type = fill_typevars ( info ) , fallback = type_type , name = None , variables = variables ,
884
+ ret_type = ret_type , fallback = type_type , name = None , variables = variables ,
874
885
special_sig = special_sig )
875
886
c = callable_type .with_name (info .name ())
876
887
return c
0 commit comments