Skip to content

Commit e92fd51

Browse files
authored
[ty] Add cycle handling to lazy_default (#20967)
1 parent c3631c7 commit e92fd51

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,23 @@ static_assert(is_subtype_of(Bottom[JsonDict], Bottom[JsonDict]))
313313
static_assert(is_subtype_of(Bottom[JsonDict], Top[JsonDict]))
314314
```
315315

316+
### Cyclic defaults
317+
318+
```py
319+
from typing_extensions import Protocol, TypeVar
320+
321+
T = TypeVar("T", default="C", covariant=True)
322+
323+
class P(Protocol[T]):
324+
pass
325+
326+
class C(P[T]):
327+
pass
328+
329+
reveal_type(C[int]()) # revealed: C[int]
330+
reveal_type(C()) # revealed: C[Divergent]
331+
```
332+
316333
### Union inside generic
317334

318335
#### With old-style union
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
spark # too many iterations (in `exported_names` query), `should not be able to access instance member `spark` of type variable IndexOpsLike@astype in inferable position`
2-
steam.py # dependency graph cycle when querying TypeVarInstance < 'db >::lazy_default_(Id(2e007)), set cycle_fn/cycle_initial to fixpoint iterate.
2+
steam.py # too many iterations

crates/ty_python_semantic/src/types.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8344,7 +8344,7 @@ impl<'db> TypeVarInstance<'db> {
83448344
Some(TypeVarBoundOrConstraints::Constraints(ty))
83458345
}
83468346

8347-
#[salsa::tracked(heap_size=ruff_memory_usage::heap_size)]
8347+
#[salsa::tracked(cycle_initial=lazy_default_cycle_initial, heap_size=ruff_memory_usage::heap_size)]
83488348
fn lazy_default(self, db: &'db dyn Db) -> Option<Type<'db>> {
83498349
let definition = self.definition(db)?;
83508350
let module = parsed_module(db, definition.file(db)).load(db);
@@ -8391,6 +8391,13 @@ fn lazy_bound_or_constraints_cycle_initial<'db>(
83918391
None
83928392
}
83938393

8394+
fn lazy_default_cycle_initial<'db>(
8395+
_db: &'db dyn Db,
8396+
_self: TypeVarInstance<'db>,
8397+
) -> Option<Type<'db>> {
8398+
None
8399+
}
8400+
83948401
/// Where a type variable is bound and usable.
83958402
#[derive(
83968403
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, salsa::Update, get_size2::GetSize,

0 commit comments

Comments
 (0)