@@ -244,15 +244,15 @@ class C(D[E[T]], Generic[T]): ...
244
244
return expand_type_by_instance (typ , inst_type )
245
245
246
246
247
- def supported_self_type (typ : ProperType ) -> bool :
247
+ def supported_self_type (typ : ProperType , allow_callable : bool = True ) -> bool :
248
248
"""Is this a supported kind of explicit self-types?
249
249
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
251
251
a type variable with an instance upper bound.
252
252
"""
253
253
if isinstance (typ , TypeType ):
254
254
return supported_self_type (typ .item )
255
- if isinstance (typ , CallableType ):
255
+ if allow_callable and isinstance (typ , CallableType ):
256
256
# Special case: allow class callable instead of Type[...] as cls annotation,
257
257
# as well as callable self for callback protocols.
258
258
return True
@@ -306,7 +306,11 @@ class B(A): pass
306
306
self_param_type = get_proper_type (func .arg_types [0 ])
307
307
308
308
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 ):
310
314
from mypy .infer import infer_type_arguments
311
315
312
316
if original_type is None :
0 commit comments