@@ -244,15 +244,15 @@ class C(D[E[T]], Generic[T]): ...
244244 return expand_type_by_instance (typ , inst_type )
245245
246246
247- def supported_self_type (typ : ProperType ) -> bool :
247+ def supported_self_type (typ : ProperType , allow_callable : bool = True ) -> bool :
248248 """Is this a supported kind of explicit self-types?
249249
250- Currently, this means a X or Type[X], where X is an instance or
250+ Currently, this means an X or Type[X], where X is an instance or
251251 a type variable with an instance upper bound.
252252 """
253253 if isinstance (typ , TypeType ):
254254 return supported_self_type (typ .item )
255- if isinstance (typ , CallableType ):
255+ if allow_callable and isinstance (typ , CallableType ):
256256 # Special case: allow class callable instead of Type[...] as cls annotation,
257257 # as well as callable self for callback protocols.
258258 return True
@@ -306,7 +306,11 @@ class B(A): pass
306306 self_param_type = get_proper_type (func .arg_types [0 ])
307307
308308 variables : Sequence [TypeVarLikeType ]
309- if func .variables and supported_self_type (self_param_type ):
309+ # Having a def __call__(self: Callable[...], ...) can cause infinite recursion. Although
310+ # this special-casing looks not very principled, there is nothing meaningful we can infer
311+ # from such definition, since it is inherently indefinitely recursive.
312+ allow_callable = func .name is None or not func .name .startswith ("__call__ of" )
313+ if func .variables and supported_self_type (self_param_type , allow_callable = allow_callable ):
310314 from mypy .infer import infer_type_arguments
311315
312316 if original_type is None :
0 commit comments