Skip to content

Commit ef0fb8f

Browse files
authored
[ty] Fix bound TypeVar default cycle recovery (#26124)
1 parent b83c024 commit ef0fb8f

3 files changed

Lines changed: 77 additions & 7 deletions

File tree

‎crates/ty_python_semantic/resources/mdtest/generics/pep695/aliases.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ Self-referential defaults should not crash type inference:
275275
type A[T = A] = A[int]
276276
```
277277

278+
An explicit alias specialization overrides the alias type parameter's default:
279+
280+
```py
281+
type ExplicitAlias[
282+
FirstT = ExplicitAlias[int, str],
283+
SecondT = FirstT,
284+
] = list[SecondT]
285+
286+
def explicit_alias_default(x: ExplicitAlias):
287+
reveal_type(x) # revealed: list[ExplicitAlias[int, str]]
288+
```
289+
278290
A self-referential default that does not reference itself in the alias body should also not crash,
279291
even when the default is evaluated (e.g., by omitting the type argument):
280292

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Regression test for #3804
2+
3+
Regression test for [this issue](https://github.com/astral-sh/ty/issues/3804).
4+
5+
`cog.py`:
6+
7+
```py
8+
from commands import GroupMixin
9+
10+
class Group(GroupMixin): ...
11+
class Bot(GroupMixin): ...
12+
```
13+
14+
`commands.py`:
15+
16+
```py
17+
from typing import TYPE_CHECKING, Generic
18+
from typing_extensions import TypeVar
19+
20+
if TYPE_CHECKING:
21+
from cog import Bot
22+
23+
CogT = TypeVar("CogT", bound="Bot", default="Bot", covariant=True)
24+
25+
class GroupMixin(Generic[CogT]):
26+
def method(self): ...
27+
28+
def call_method(value: object):
29+
if isinstance(value, GroupMixin):
30+
value.method()
31+
```

‎crates/ty_python_semantic/src/types/typevar.rs‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,30 @@ impl<'db> TypeVarInstance<'db> {
391391
return false;
392392
}
393393

394-
type_is_self_referential_impl(state, type_alias.raw_value_type(state.db), self_identity)
394+
let value_type = if let Some(specialization) = type_alias.specialization(state.db) {
395+
if specialization
396+
.types(state.db)
397+
.iter()
398+
.any(|ty| type_is_self_referential_impl(state, *ty, self_identity))
399+
{
400+
return true;
401+
}
402+
type_alias.value_type(state.db)
403+
} else if let Some(generic_context) = type_alias.generic_context(state.db)
404+
&& generic_context.variables(state.db).any(|typevar| {
405+
typevar_default_is_self_referential(
406+
state,
407+
typevar.typevar(state.db),
408+
self_identity,
409+
)
410+
})
411+
{
412+
return true;
413+
} else {
414+
type_alias.raw_value_type(state.db)
415+
};
416+
417+
type_is_self_referential_impl(state, value_type, self_identity)
395418
}
396419

397420
fn type_is_self_referential_impl<'db>(
@@ -1413,7 +1436,7 @@ impl<'db> BoundTypeVarIdentity<'db> {
14131436
}
14141437

14151438
#[salsa::tracked(
1416-
cycle_initial=|_, _, _| None,
1439+
cycle_initial=|_, id, _| Some(Type::divergent(id)),
14171440
cycle_fn=bound_typevar_default_type_cycle_recover,
14181441
heap_size=ruff_memory_usage::heap_size
14191442
)]
@@ -1433,13 +1456,17 @@ fn bound_typevar_default_type<'db>(
14331456

14341457
#[expect(clippy::ref_option)]
14351458
fn bound_typevar_default_type_cycle_recover<'db>(
1436-
_db: &'db dyn Db,
1437-
_cycle: &salsa::Cycle,
1438-
_previous_default: &Option<Type<'db>>,
1439-
_default: Option<Type<'db>>,
1459+
db: &'db dyn Db,
1460+
cycle: &salsa::Cycle,
1461+
previous_default: &Option<Type<'db>>,
1462+
default: Option<Type<'db>>,
14401463
_bound_typevar: BoundTypeVarInstance<'db>,
14411464
) -> Option<Type<'db>> {
1442-
None
1465+
match (previous_default, default) {
1466+
(Some(previous), Some(default)) => Some(default.cycle_normalized(db, *previous, cycle)),
1467+
(None, Some(default)) => Some(default.recursive_type_normalized(db, cycle)),
1468+
(_, None) => None,
1469+
}
14431470
}
14441471

14451472
/// Whether a typevar default is eagerly specified or lazily evaluated.

0 commit comments

Comments
 (0)