mirror of
https://github.com/astral-sh/ruff
synced 2026-01-08 15:14:19 -05:00
[ty] Add cycle handling to lazy_default (#20967)
This commit is contained in:
@@ -313,6 +313,23 @@ static_assert(is_subtype_of(Bottom[JsonDict], Bottom[JsonDict]))
|
||||
static_assert(is_subtype_of(Bottom[JsonDict], Top[JsonDict]))
|
||||
```
|
||||
|
||||
### Cyclic defaults
|
||||
|
||||
```py
|
||||
from typing_extensions import Protocol, TypeVar
|
||||
|
||||
T = TypeVar("T", default="C", covariant=True)
|
||||
|
||||
class P(Protocol[T]):
|
||||
pass
|
||||
|
||||
class C(P[T]):
|
||||
pass
|
||||
|
||||
reveal_type(C[int]()) # revealed: C[int]
|
||||
reveal_type(C()) # revealed: C[Divergent]
|
||||
```
|
||||
|
||||
### Union inside generic
|
||||
|
||||
#### With old-style union
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
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`
|
||||
steam.py # dependency graph cycle when querying TypeVarInstance < 'db >::lazy_default_(Id(2e007)), set cycle_fn/cycle_initial to fixpoint iterate.
|
||||
steam.py # too many iterations
|
||||
|
||||
@@ -8344,7 +8344,7 @@ impl<'db> TypeVarInstance<'db> {
|
||||
Some(TypeVarBoundOrConstraints::Constraints(ty))
|
||||
}
|
||||
|
||||
#[salsa::tracked(heap_size=ruff_memory_usage::heap_size)]
|
||||
#[salsa::tracked(cycle_initial=lazy_default_cycle_initial, heap_size=ruff_memory_usage::heap_size)]
|
||||
fn lazy_default(self, db: &'db dyn Db) -> Option<Type<'db>> {
|
||||
let definition = self.definition(db)?;
|
||||
let module = parsed_module(db, definition.file(db)).load(db);
|
||||
@@ -8391,6 +8391,13 @@ fn lazy_bound_or_constraints_cycle_initial<'db>(
|
||||
None
|
||||
}
|
||||
|
||||
fn lazy_default_cycle_initial<'db>(
|
||||
_db: &'db dyn Db,
|
||||
_self: TypeVarInstance<'db>,
|
||||
) -> Option<Type<'db>> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Where a type variable is bound and usable.
|
||||
#[derive(
|
||||
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, salsa::Update, get_size2::GetSize,
|
||||
|
||||
Reference in New Issue
Block a user