Expand a generic callable using all combinations of type variables' values/bounds.
(c: CallableType)
| 9017 | |
| 9018 | |
| 9019 | def 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 | |
| 9048 | def is_unsafe_overlapping_overload_signatures( |
no test coverage detected
searching dependent graphs…