Skip to content

Commit db1f00b

Browse files
jopemachineclaude
andcommitted
refactor: use typing.Self on ModelDeploymentStatus.from_lifecycle
Address review comment — switch the explicit forward reference return annotation to ``Self`` so subclasses get the proper return type without the string literal. Route through the enum constructor (``cls(name)``) with a small lookup table so mypy can keep the ``Self`` return type without choking on the narrower ``Literal[ModelDeploymentStatus.X]`` inferred from direct ``cls.X`` returns. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 4b8f701 commit db1f00b

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

  • src/ai/backend/common/data/model_deployment

src/ai/backend/common/data/model_deployment/types.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ class ActivenessStatus(CIStrEnum):
3232
"created": "PENDING", # never-deployed initial state
3333
}
3434

35+
# Project ``EndpointLifecycle`` onto the v2 status name fed into the enum
36+
# constructor. ``SCALING`` lifecycle folds into ``READY`` because replica
37+
# reconciliation is exposed on the orthogonal ``scaling_state`` axis instead
38+
# of the lifecycle axis; ``CREATED`` (never-deployed) folds into ``PENDING``.
39+
_LIFECYCLE_TO_DEPLOYMENT_STATUS_NAME: dict[EndpointLifecycle, str] = {
40+
EndpointLifecycle.PENDING: "PENDING",
41+
EndpointLifecycle.CREATED: "PENDING",
42+
EndpointLifecycle.READY: "READY",
43+
EndpointLifecycle.SCALING: "READY",
44+
EndpointLifecycle.DEPLOYING: "DEPLOYING",
45+
EndpointLifecycle.DESTROYING: "STOPPING",
46+
EndpointLifecycle.DESTROYED: "STOPPED",
47+
}
48+
3549

3650
class ModelDeploymentStatus(CIStrEnum):
3751
PENDING = "PENDING"
@@ -50,18 +64,8 @@ def _missing_(cls, value: Any) -> Self | None:
5064
return super()._missing_(value)
5165

5266
@classmethod
53-
def from_lifecycle(cls, lifecycle: EndpointLifecycle) -> "ModelDeploymentStatus":
54-
match lifecycle:
55-
case EndpointLifecycle.PENDING | EndpointLifecycle.CREATED:
56-
return cls.PENDING
57-
case EndpointLifecycle.READY | EndpointLifecycle.SCALING:
58-
return cls.READY
59-
case EndpointLifecycle.DEPLOYING:
60-
return cls.DEPLOYING
61-
case EndpointLifecycle.DESTROYING:
62-
return cls.STOPPING
63-
case EndpointLifecycle.DESTROYED:
64-
return cls.STOPPED
67+
def from_lifecycle(cls, lifecycle: EndpointLifecycle) -> Self:
68+
return cls(_LIFECYCLE_TO_DEPLOYMENT_STATUS_NAME[lifecycle])
6569

6670

6771
class DeploymentStrategy(CIStrEnum):

0 commit comments

Comments
 (0)