@@ -650,18 +650,28 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
650
650
where ... are argument types for the __init__/__new__ method (without the self
651
651
argument). Also, the fallback type will be 'type' instead of 'function'.
652
652
"""
653
+
654
+ # We take the type from whichever of __init__ and __new__ is first
655
+ # in the MRO, preferring __init__ if there is a tie.
653
656
init_method = info .get_method ('__init__' )
657
+ new_method = info .get_method ('__new__' )
654
658
if not init_method :
655
659
# Must be an invalid class definition.
656
660
return AnyType (TypeOfAny .from_error )
661
+ # There *should* always be a __new__ method except the test stubs
662
+ # lack it, so just copy init_method in that situation
663
+ new_method = new_method or init_method
664
+
665
+ init_index = info .mro .index (init_method .info )
666
+ new_index = info .mro .index (new_method .info )
667
+
668
+ fallback = info .metaclass_type or builtin_type ('builtins.type' )
669
+ if init_index < new_index :
670
+ method = init_method
671
+ elif init_index > new_index :
672
+ method = new_method
657
673
else :
658
- fallback = info .metaclass_type or builtin_type ('builtins.type' )
659
674
if init_method .info .fullname () == 'builtins.object' :
660
- # No non-default __init__ -> look at __new__ instead.
661
- new_method = info .get_method ('__new__' )
662
- if new_method and new_method .info .fullname () != 'builtins.object' :
663
- # Found one! Get signature from __new__.
664
- return type_object_type_from_function (new_method , info , fallback )
665
675
# Both are defined by object. But if we've got a bogus
666
676
# base class, we can't know for sure, so check for that.
667
677
if info .fallback_to_any :
@@ -673,9 +683,14 @@ def type_object_type(info: TypeInfo, builtin_type: Callable[[str], Instance]) ->
673
683
ret_type = any_type ,
674
684
fallback = builtin_type ('builtins.function' ))
675
685
return class_callable (sig , info , fallback , None )
676
- # Construct callable type based on signature of __init__. Adjust
677
- # return type and insert type arguments.
678
- return type_object_type_from_function (init_method , info , fallback )
686
+
687
+ # Otherwise prefer __init__ in a tie. It isn't clear that this
688
+ # is the right thing, but __new__ caused problems with
689
+ # typeshed (#5647).
690
+ method = init_method
691
+ # Construct callable type based on signature of __init__. Adjust
692
+ # return type and insert type arguments.
693
+ return type_object_type_from_function (method , info , fallback )
679
694
680
695
681
696
def type_object_type_from_function (init_or_new : FuncBase , info : TypeInfo ,
0 commit comments