Fix pre-existing type errors in config_editor

`_require_repo()` now returns the validated repo root (Path) instead of
None, so callers get a non-None path and pyright stops flagging `root /
spec.source` and the `CastleConfig(root=root)` construction. Drops the
redundant `get_castle_root()` re-fetch after each guard and hoists the
`pathlib.Path` import to module scope.
This commit is contained in:
2026-06-29 07:11:22 -07:00
parent b105487790
commit 12dea5651c

View File

@@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from pathlib import Path
import yaml import yaml
from fastapi import APIRouter, HTTPException, status from fastapi import APIRouter, HTTPException, status
@@ -58,13 +59,15 @@ class JobConfigRequest(BaseModel):
config: dict config: dict
def _require_repo() -> None: def _require_repo() -> Path:
"""Raise 503 if repo is not available.""" """Return the castle repo root, or raise 503 if it's not available."""
if get_castle_root() is None: root = get_castle_root()
if root is None:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Castle repo not available on this node.", detail="Castle repo not available on this node.",
) )
return root
def _aggregate_yaml(config: CastleConfig) -> str: def _aggregate_yaml(config: CastleConfig) -> str:
@@ -88,8 +91,7 @@ def _aggregate_yaml(config: CastleConfig) -> str:
@router.get("", response_model=ConfigResponse) @router.get("", response_model=ConfigResponse)
def get_config_yaml() -> ConfigResponse: def get_config_yaml() -> ConfigResponse:
"""Get a unified virtual castle.yaml aggregated from all resource files.""" """Get a unified virtual castle.yaml aggregated from all resource files."""
_require_repo() root = _require_repo()
root = get_castle_root()
config = load_config(root) config = load_config(root)
return ConfigResponse(yaml_content=_aggregate_yaml(config)) return ConfigResponse(yaml_content=_aggregate_yaml(config))
@@ -97,8 +99,7 @@ def get_config_yaml() -> ConfigResponse:
@router.put("", response_model=ConfigSaveResponse) @router.put("", response_model=ConfigSaveResponse)
def save_yaml(request: ConfigSaveRequest) -> ConfigSaveResponse: def save_yaml(request: ConfigSaveRequest) -> ConfigSaveResponse:
"""Validate and save castle.yaml. Does NOT apply changes.""" """Validate and save castle.yaml. Does NOT apply changes."""
_require_repo() root = _require_repo()
root = get_castle_root()
errors: list[str] = [] errors: list[str] = []
# Parse YAML # Parse YAML
@@ -119,8 +120,6 @@ def save_yaml(request: ConfigSaveRequest) -> ConfigSaveResponse:
# repo: drives repo-relative source resolution (fall back to existing config) # repo: drives repo-relative source resolution (fall back to existing config)
repo_path = None repo_path = None
if data.get("repo"): if data.get("repo"):
from pathlib import Path
repo_path = Path(data["repo"]).expanduser() repo_path = Path(data["repo"]).expanduser()
else: else:
try: try:
@@ -129,8 +128,6 @@ def save_yaml(request: ConfigSaveRequest) -> ConfigSaveResponse:
repo_path = None repo_path = None
def _resolve_source(spec: ProgramSpec) -> None: def _resolve_source(spec: ProgramSpec) -> None:
from pathlib import Path
if not spec.source: if not spec.source:
return return
if spec.source.startswith("repo:") and repo_path: if spec.source.startswith("repo:") and repo_path: