Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -9246,20 +9254,44 @@ 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:
tvar_values.append([tvar.upper_bound])

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,
Expand Down
42 changes: 42 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -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"
37 changes: 37 additions & 0 deletions test-data/unit/check-typevar-values.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Loading