@@ -177,12 +177,15 @@ def type_object_type(info: TypeInfo, named_type: Callable[[str], Instance]) -> P
177177 init_index = info .mro .index (init_method .node .info )
178178 new_index = info .mro .index (new_method .node .info )
179179
180- if checker_state .type_checker :
181- builtins_type = checker_state .type_checker .named_type ("builtins.type" )
180+ if info .metaclass_type is not None :
181+ fallback = info .metaclass_type
182+ elif checker_state .type_checker :
183+ # Prefer direct call when it is available. It is faster, and,
184+ # unfortunately, some callers provide bogus callback.
185+ fallback = checker_state .type_checker .named_type ("builtins.type" )
182186 else :
183- builtins_type = named_type ("builtins.type" )
187+ fallback = named_type ("builtins.type" )
184188
185- fallback = info .metaclass_type or builtins_type
186189 if init_index < new_index :
187190 method : FuncBase | Decorator = init_method .node
188191 is_new = False
@@ -218,6 +221,8 @@ def type_object_type(info: TypeInfo, named_type: Callable[[str], Instance]) -> P
218221 # return type and insert type arguments.
219222 if isinstance (method , FuncBase ):
220223 if isinstance (method , OverloadedFuncDef ) and not method .type :
224+ # Do not cache if the type is not ready. Same logic for decorators is
225+ # achieved in early return above because is_valid_constructor() is False.
221226 allow_cache = False
222227 t = function_type (method , fallback )
223228 else :
0 commit comments