diff --git a/mypy/checker.py b/mypy/checker.py index 3f2a8476ed934..528e70f220882 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -248,6 +248,7 @@ def __init__(self) -> None: false_only, fixup_partial_type, function_type, + get_all_type_vars, get_tuple_item_types, is_literal_type_like, is_singleton_equality_type, @@ -2311,6 +2312,13 @@ def expand_typevars( if defn.info: # Class type variables tvars += defn.info.defn.type_vars or [] + if is_trivial_body(defn.body): + # Only type variables that appear in the signature can make a difference + # for a trivial body. Skipping the other ones avoids a combinatorial explosion, + # e.g. for methods with an explicit self type in a class with many + # value-restricted type variables (this is common in generated stubs). + used_ids = get_type_var_ids_in_callable(typ) + tvars = [tv for tv in tvars if tv.id in used_ids] for tvar in tvars: if isinstance(tvar, TypeVarType) and tvar.values: subst.append([(tvar.id, value) for value in tvar.values]) @@ -9246,8 +9254,14 @@ def expand_callable_variants(c: CallableType) -> list[CallableType]: # Fast path. return [c] + # Substituting a type variable that doesn't appear in the callable has no effect, but + # each such variable with values would still multiply the number of (identical) variants. + # Note that detach_callable() adds all class type variables, even if only few are used. + used_ids = get_type_var_ids_in_callable(c) + variables = [tv for tv in c.variables if tv.id in used_ids] + tvar_values = [] - for tvar in c.variables: + for tvar in variables: if isinstance(tvar, TypeVarType) and tvar.values: tvar_values.append(tvar.values) else: @@ -9255,11 +9269,29 @@ def expand_callable_variants(c: CallableType) -> list[CallableType]: variants = [] for combination in itertools.product(*tvar_values): - tvar_map = {tv.id: subst for (tv, subst) in zip(c.variables, combination)} + tvar_map = {tv.id: subst for (tv, subst) in zip(variables, combination)} variants.append(expand_type(c, tvar_map).copy_modified(variables=[])) return variants +def get_type_var_ids_in_callable(c: CallableType) -> set[TypeVarId]: + """Find ids of all type variables used in a callable type. + + This also includes type variables used in bounds or values of the callable's own + type variables (this is only relevant for self-types). + """ + types: list[Type] = [c] + if c.type_guard is not None: + types.append(c.type_guard) + if c.type_is is not None: + types.append(c.type_is) + for tv in c.variables: + types.append(tv.upper_bound) + if isinstance(tv, TypeVarType): + types.extend(tv.values) + return {tv.id for t in types for tv in get_all_type_vars(t)} + + def is_unsafe_overlapping_overload_signatures( signature: CallableType, other: CallableType, diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index cd5b924ed6b49..1144caf0c7f67 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -7046,3 +7046,45 @@ reveal_type(f(la)) # N: Revealed type is "builtins.list[builtins.tuple[Any, ... reveal_type(f(li)) # N: Revealed type is "builtins.list[builtins.tuple[builtins.int, ...]]" reveal_type(f(ls)) # N: Revealed type is "builtins.list[builtins.tuple[Any, ...]]" [builtins fixtures/tuple.pyi] + +[case testOverloadManyValueRestrictedClassTypeVarsExplicitSelf] +# This used to take time and memory exponential in the number of type variables. +import stub +reveal_type(stub.C(1).f(1)) +[file stub.pyi] +from typing import Generic, TypeVar, overload + +T0 = TypeVar("T0", int, str) +T1 = TypeVar("T1", int, str) +T2 = TypeVar("T2", int, str) +T3 = TypeVar("T3", int, str) +T4 = TypeVar("T4", int, str) +T5 = TypeVar("T5", int, str) +T6 = TypeVar("T6", int, str) +T7 = TypeVar("T7", int, str) +T8 = TypeVar("T8", int, str) +T9 = TypeVar("T9", int, str) +T10 = TypeVar("T10", int, str) +T11 = TypeVar("T11", int, str) +T12 = TypeVar("T12", int, str) +T13 = TypeVar("T13", int, str) +T14 = TypeVar("T14", int, str) +T15 = TypeVar("T15", int, str) +T16 = TypeVar("T16", int, str) +T17 = TypeVar("T17", int, str) +T18 = TypeVar("T18", int, str) +T19 = TypeVar("T19", int, str) + +class C(Generic[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19]): + @overload + def __init__(self: C[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int], x: int) -> None: ... + @overload + def __init__(self: C[str, str, str, str, str, str, str, str, str, str, str, str, str, str, str, str, str, str, str, str], x: str) -> None: ... + @overload + def f(self: C[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int], x: int) -> int: ... + @overload + def f(self: C[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int], x: object) -> str: ... +[builtins fixtures/tuple.pyi] +[out] +tmp/stub.pyi:30: error: Overloaded function signatures 1 and 2 overlap with incompatible return types +main:3: note: Revealed type is "builtins.int" diff --git a/test-data/unit/check-typevar-values.test b/test-data/unit/check-typevar-values.test index e0ebc6ddf13ae..eb1d27816776a 100644 --- a/test-data/unit/check-typevar-values.test +++ b/test-data/unit/check-typevar-values.test @@ -758,3 +758,40 @@ def g(x: S = c): # OK ... def h(x: S = b): # E: Incompatible default for parameter "x" (default has type "B", parameter has type "S") ... + +[case testManyValueRestrictedClassTypeVarsTrivialBody] +# This used to take time and memory exponential in the number of type variables. +from typing import Generic, TypeVar + +T0 = TypeVar("T0", int, str) +T1 = TypeVar("T1", int, str) +T2 = TypeVar("T2", int, str) +T3 = TypeVar("T3", int, str) +T4 = TypeVar("T4", int, str) +T5 = TypeVar("T5", int, str) +T6 = TypeVar("T6", int, str) +T7 = TypeVar("T7", int, str) +T8 = TypeVar("T8", int, str) +T9 = TypeVar("T9", int, str) +T10 = TypeVar("T10", int, str) +T11 = TypeVar("T11", int, str) +T12 = TypeVar("T12", int, str) +T13 = TypeVar("T13", int, str) +T14 = TypeVar("T14", int, str) +T15 = TypeVar("T15", int, str) +T16 = TypeVar("T16", int, str) +T17 = TypeVar("T17", int, str) +T18 = TypeVar("T18", int, str) +T19 = TypeVar("T19", int, str) + +class C(Generic[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19]): + def f(self: C[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int], x: int) -> None: ... + def g(self: C[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int], x: int) -> None: + pass + +class D(Generic[T0, T1]): + def f(self: D[int, int]) -> None: + x: T0 + reveal_type(x) # N: Revealed type is "builtins.int" \ + # N: Revealed type is "builtins.str" +[builtins fixtures/tuple.pyi]