Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,16 @@ type X[T: X] = T
def _(x: X):
assert x
```

## Recursive generic type aliases

```py
type RecursiveList[T] = T | list[RecursiveList[T]]

r1: RecursiveList[int] = 1
r2: RecursiveList[int] = [1, [1, 2, 3]]
# error: [invalid-assignment] "Object of type `Literal["a"]` is not assignable to `RecursiveList[int]`"
r3: RecursiveList[int] = "a"
# TODO: this should be an error
r4: RecursiveList[int] = [1, ["a"]]
```
43 changes: 32 additions & 11 deletions crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6791,7 +6791,20 @@ impl<'db> Type<'db> {
Type::TypeIs(type_is) => type_is.with_type(db, type_is.return_type(db).apply_type_mapping(db, type_mapping, tcx)),

Type::TypeAlias(alias) => {
visitor.visit(self, || alias.value_type(db).apply_type_mapping_impl(db, type_mapping, tcx, visitor))
// Do not call `value_type` here. `value_type` does the specialization internally, so `apply_type_mapping` is performed without `visitor` inheritance.
// In the case of recursive type aliases, this leads to infinite recursion.
// Instead, call `raw_value_type` and perform the specialization after the `visitor` cache has been created.
let value_type = visitor.visit(self, || alias.raw_value_type(db).apply_type_mapping_impl(db, type_mapping, tcx, visitor));
if let Some(generic_context) = alias.generic_context(db) {
let specialization = alias
.specialization(db)
.unwrap_or_else(|| generic_context.default_specialization(db, None));

value_type.apply_specialization(db, specialization)
.apply_type_mapping_impl(db, type_mapping, tcx, visitor)
} else {
value_type
}
}

Type::ModuleLiteral(_)
Expand Down Expand Up @@ -10718,7 +10731,6 @@ impl<'db> PEP695TypeAliasType<'db> {
}

/// The RHS type of a PEP-695 style type alias with specialization applied.
#[salsa::tracked(cycle_fn=value_type_cycle_recover, cycle_initial=value_type_cycle_initial, heap_size=ruff_memory_usage::heap_size)]
pub(crate) fn value_type(self, db: &'db dyn Db) -> Type<'db> {
let value_type = self.raw_value_type(db);

Expand All @@ -10734,15 +10746,7 @@ impl<'db> PEP695TypeAliasType<'db> {
}

/// The RHS type of a PEP-695 style type alias with *no* specialization applied.
///
/// ## Warning
///
/// This uses the semantic index to find the definition of the type alias. This means that if the
/// calling query is not in the same file as this type alias is defined in, then this will create
/// a cross-module dependency directly on the full AST which will lead to cache
/// over-invalidation.
/// This method also calls the type inference functions, and since type aliases can have recursive structures,
/// we should be careful not to create infinite recursions in this method (or make it tracked if necessary).
#[salsa::tracked(cycle_fn=value_type_cycle_recover, cycle_initial=value_type_cycle_initial, heap_size=ruff_memory_usage::heap_size)]
pub(crate) fn raw_value_type(self, db: &'db dyn Db) -> Type<'db> {
let scope = self.rhs_scope(db);
let module = parsed_module(db, scope.file(db)).load(db);
Expand Down Expand Up @@ -11820,6 +11824,9 @@ type CovariantAlias[T] = Covariant[T]
type ContravariantAlias[T] = Contravariant[T]
type InvariantAlias[T] = Invariant[T]
type BivariantAlias[T] = Bivariant[T]

type RecursiveAlias[T] = None | list[RecursiveAlias[T]]
type RecursiveAlias2[T] = None | list[T] | list[RecursiveAlias2[T]]
"#,
)
.unwrap();
Expand Down Expand Up @@ -11850,5 +11857,19 @@ type BivariantAlias[T] = Bivariant[T]
.variance_of(&db, get_bound_typevar(&db, bivariant)),
TypeVarVariance::Bivariant
);

let recursive = get_type_alias(&db, "RecursiveAlias");
assert_eq!(
KnownInstanceType::TypeAliasType(TypeAliasType::PEP695(recursive))
.variance_of(&db, get_bound_typevar(&db, recursive)),
TypeVarVariance::Bivariant
);

let recursive2 = get_type_alias(&db, "RecursiveAlias2");
assert_eq!(
KnownInstanceType::TypeAliasType(TypeAliasType::PEP695(recursive2))
.variance_of(&db, get_bound_typevar(&db, recursive2)),
TypeVarVariance::Invariant
);
}
}