Skip to content

Commit 2324c77

Browse files
fregataaclaude
andcommitted
refactor(BA-5936): make CascadeChild generic over its row type
Parameterize CascadeChild on the cascade table's Row class (``CascadeChild[TRow: Base]``) for consistency with PrunerSpec[TRow] and to give subclass authors precise typing on ``row_class()`` — e.g., ``CascadeChild[KernelRow]`` makes ``row_class()`` return ``type[KernelRow]`` instead of the over-broad ``type[Base]``. PrunerSpec.cascade is typed as ``list[CascadeChild[Any]]`` since a spec composes cascades over heterogeneous tables. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f23a963 commit 2324c77

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

src/ai/backend/manager/repositories/base/pruner.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
TRow = TypeVar("TRow", bound=Base)
2525

2626

27-
class CascadeChild(ABC):
27+
class CascadeChild[TRow: Base](ABC):
2828
"""A child table whose rows must be deleted before the parent's prune.
2929
3030
Used for simple FK cascades. ``execute_pruner`` first locks and
@@ -36,13 +36,16 @@ class CascadeChild(ABC):
3636
where ``<target_ids>`` is the list returned from the single
3737
``SELECT pk FOR UPDATE`` against the parent table.
3838
39+
The type parameter ``TRow`` is the cascade table's ORM Row class —
40+
e.g., ``CascadeChild[KernelRow]``.
41+
3942
Polymorphic / cross-cutting cleanups (e.g., RBAC associations) are not
4043
handled here — see :meth:`PrunerSpec.entity_type` for that.
4144
"""
4245

4346
@classmethod
4447
@abstractmethod
45-
def row_class(cls) -> type[Base]:
48+
def row_class(cls) -> type[TRow]:
4649
"""ORM Row class for the cascade table.
4750
4851
Example:
@@ -89,7 +92,7 @@ class PrunerSpec[TRow: Base](ABC):
8992
"""
9093

9194
conditions: list[QueryCondition] = field(default_factory=list)
92-
cascade: list[CascadeChild] = field(default_factory=list)
95+
cascade: list[CascadeChild[Any]] = field(default_factory=list)
9396
limit: int = DEFAULT_PRUNE_LIMIT
9497
cascade_rbac: bool = True
9598

tests/unit/manager/repositories/base/test_pruner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class PrunerTestChildRow(Base): # type: ignore[misc]
6060
name = sa.Column(sa.String(50), nullable=False)
6161

6262

63-
class TestChildCascade(CascadeChild):
63+
class TestChildCascade(CascadeChild[PrunerTestChildRow]):
6464
@classmethod
65-
def row_class(cls) -> type[Base]:
65+
def row_class(cls) -> type[PrunerTestChildRow]:
6666
return PrunerTestChildRow
6767

6868
@classmethod

0 commit comments

Comments
 (0)