Skip to content

Commit 47ca4c2

Browse files
authored
Fix various crashes on recursive type variable defaults (#21491)
Fixes #17716 Fixes #20698 Fixes #21128 Fixes #21269 Although the main idea is relatively simple (replace explicit type variable defaults with `Any` in cases where they would cause infinite recursion), this required a lot of "plumbing", because: * We always call `fix_instance()` eagerly and early. * There are four different cases to handle (classes vs aliases, and old style vs new style). * Relevant pieces of information are available in "distant" places (and I don't want to add any global state). Some additional notes: * I clean-up some weirdness with deferral on type variable defaults with placeholders (especially in type aliases). * I remove an unwarranted `get_proper_type()` call during semantic analysis. * Now we handle rare edge case of unused alias type variables in `BoolTypeQuery` (possible with new style aliases). * Type variable defaults should be considered in `__eq__()` to correctly detect progress.
1 parent 37ee432 commit 47ca4c2

12 files changed

Lines changed: 513 additions & 110 deletions

‎mypy/checker.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8529,6 +8529,9 @@ def lookup_fully_qualified_or_none(self, fullname: str, /) -> SymbolTableNode |
85298529
except KeyError:
85308530
return None
85318531

8532+
def record_fixed_type(self, fixed: TypeInfo | TypeAlias) -> None:
8533+
pass
8534+
85328535
def fail(
85338536
self,
85348537
msg: str,

‎mypy/checkexpr.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4938,10 +4938,11 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
49384938
if tapp.expr.node.python_3_12_type_alias:
49394939
return self.type_alias_type_type()
49404940
# Subscription of a (generic) alias in runtime context, expand the alias.
4941-
item = instantiate_type_alias(
4941+
item, _ = instantiate_type_alias(
49424942
tapp.expr.node,
49434943
tapp.types,
49444944
self.chk.fail,
4945+
self.chk.note,
49454946
tapp.expr.node.no_args,
49464947
tapp,
49474948
self.chk.options,
@@ -5006,17 +5007,16 @@ class LongName(Generic[T]): ...
50065007
# A = List[Tuple[T, T]]
50075008
# x = A() <- same as List[Tuple[Any, Any]], see PEP 484.
50085009
disallow_any = self.chk.options.disallow_any_generics and self.is_callee
5009-
item = get_proper_type(
5010-
set_any_tvars(
5011-
alias,
5012-
[],
5013-
ctx.line,
5014-
ctx.column,
5015-
self.chk.options,
5016-
disallow_any=disallow_any,
5017-
fail=self.msg.fail,
5018-
)
5010+
item, _ = set_any_tvars(
5011+
alias,
5012+
[],
5013+
ctx.line,
5014+
ctx.column,
5015+
self.chk.options,
5016+
disallow_any=disallow_any,
5017+
fail=self.msg.fail,
50195018
)
5019+
item = get_proper_type(item)
50205020
if isinstance(item, Instance):
50215021
# Normally we get a callable type (or overloaded) with .is_type_obj() true
50225022
# representing the class's constructor

‎mypy/message_registry.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
180180
IMPLICIT_GENERIC_ANY_BUILTIN: Final = (
181181
'Implicit generic "Any". Use "{}" and specify generic parameters'
182182
)
183+
NO_CYCLIC_DEFAULT: Final = "Cyclic type variable defaults are not supported"
183184
INVALID_UNPACK: Final = "{} cannot be unpacked (must be tuple or TypeVarTuple)"
184185
INVALID_UNPACK_POSITION: Final = "Unpack is only valid in a variadic position"
185186
INVALID_PARAM_SPEC_LOCATION: Final = "Invalid location for ParamSpec {}"

‎mypy/nodes.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3145,7 +3145,15 @@ class TypeVarLikeExpr(SymbolNode, Expression):
31453145
Note that they are constructed by the semantic analyzer.
31463146
"""
31473147

3148-
__slots__ = ("_name", "_fullname", "upper_bound", "default", "variance", "is_new_style")
3148+
__slots__ = (
3149+
"_name",
3150+
"_fullname",
3151+
"upper_bound",
3152+
"default",
3153+
"variance",
3154+
"is_new_style",
3155+
"default_depends",
3156+
)
31493157

31503158
_name: str
31513159
_fullname: str
@@ -3160,6 +3168,9 @@ class TypeVarLikeExpr(SymbolNode, Expression):
31603168
# TypeVar(..., contravariant=True) defines a contravariant type
31613169
# variable.
31623170
variance: int
3171+
# Record instances and type aliases that appear bare/implicit in the default value
3172+
# of this type variable. This is needed to detect recursive type variable defaults.
3173+
default_depends: set[TypeInfo | TypeAlias] | None
31633174

31643175
def __init__(
31653176
self,
@@ -3178,6 +3189,7 @@ def __init__(
31783189
self.default = default
31793190
self.variance = variance
31803191
self.is_new_style = is_new_style
3192+
self.default_depends = None
31813193

31823194
@property
31833195
def name(self) -> str:
@@ -3655,6 +3667,7 @@ class is generic then it will be a type constructor of higher kind.
36553667
"is_type_check_only",
36563668
"deprecated",
36573669
"type_object_type",
3670+
"default_depends",
36583671
)
36593672

36603673
_fullname: str # Fully qualified name
@@ -3816,6 +3829,16 @@ class is generic then it will be a type constructor of higher kind.
38163829
# appears in runtime context.
38173830
type_object_type: mypy.types.FunctionLike | None
38183831

3832+
# Type variables whose defaults depend on defaults of type variables in other classes
3833+
# and type aliases. We keep track of this to safely handle situations like this one:
3834+
# class C[T = D]: ...
3835+
# class D[S = C]: ...
3836+
# x: C
3837+
# Since we apply fix_instance() eagerly, inferring a precise type is quite tricky.
3838+
# Therefore, we infer the type of `x` as `C[D[Any]]` to avoid infinite recursion.
3839+
# Keys are type variable full names.
3840+
default_depends: dict[str, set[TypeAlias | TypeInfo]]
3841+
38193842
FLAGS: Final = [
38203843
"is_abstract",
38213844
"is_enum",
@@ -3877,6 +3900,7 @@ def __init__(self, names: SymbolTable, defn: ClassDef, module_name: str) -> None
38773900
self.is_type_check_only = False
38783901
self.deprecated = None
38793902
self.type_object_type = None
3903+
self.default_depends = {}
38803904

38813905
def add_type_vars(self) -> None:
38823906
self.has_type_var_tuple_type = False
@@ -4548,6 +4572,7 @@ def f(x: B[T]) -> T: ... # without T, Any would be used here
45484572
"eager",
45494573
"tvar_tuple_index",
45504574
"python_3_12_type_alias",
4575+
"default_depends",
45514576
)
45524577

45534578
__match_args__ = ("name", "target", "alias_tvars", "no_args")
@@ -4580,6 +4605,8 @@ def __init__(
45804605
self.eager = eager
45814606
self.python_3_12_type_alias = python_3_12_type_alias
45824607
self.tvar_tuple_index = None
4608+
# This plays the same role as TypeInfo.default_depends attribute.
4609+
self.default_depends: dict[str, set[TypeAlias | TypeInfo]] = {}
45834610
for i, t in enumerate(alias_tvars):
45844611
if isinstance(t, mypy.types.TypeVarTupleType):
45854612
self.tvar_tuple_index = i

0 commit comments

Comments
 (0)