MCPcopy
hub / github.com/python/mypy / expand_callable_variants

Function expand_callable_variants

mypy/checker.py:9019–9045  ·  view source on GitHub ↗

Expand a generic callable using all combinations of type variables' values/bounds.

(c: CallableType)

Source from the content-addressed store, hash-verified

9017
9018
9019def expand_callable_variants(c: CallableType) -> list[CallableType]:
9020 """Expand a generic callable using all combinations of type variables' values/bounds."""
9021 for tv in c.variables:
9022 # We need to expand self-type before other variables, because this is the only
9023 # type variable that can have other type variables in the upper bound.
9024 if tv.id.is_self():
9025 c = expand_type(c, {tv.id: tv.upper_bound}).copy_modified(
9026 variables=[v for v in c.variables if not v.id.is_self()]
9027 )
9028 break
9029
9030 if not c.is_generic():
9031 # Fast path.
9032 return [c]
9033
9034 tvar_values = []
9035 for tvar in c.variables:
9036 if isinstance(tvar, TypeVarType) and tvar.values:
9037 tvar_values.append(tvar.values)
9038 else:
9039 tvar_values.append([tvar.upper_bound])
9040
9041 variants = []
9042 for combination in itertools.product(*tvar_values):
9043 tvar_map = {tv.id: subst for (tv, subst) in zip(c.variables, combination)}
9044 variants.append(expand_type(c, tvar_map).copy_modified(variables=[]))
9045 return variants
9046
9047
9048def is_unsafe_overlapping_overload_signatures(

Calls 7

expand_typeFunction · 0.90
isinstanceFunction · 0.85
zipFunction · 0.85
is_selfMethod · 0.80
appendMethod · 0.80
copy_modifiedMethod · 0.45
is_genericMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…