From 12dea5651cc0e0778010216d8d71e196c5b5d8a8 Mon Sep 17 00:00:00 2001 From: Paul Payne Date: Mon, 29 Jun 2026 07:11:22 -0700 Subject: [PATCH] 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. --- castle-api/src/castle_api/config_editor.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/castle-api/src/castle_api/config_editor.py b/castle-api/src/castle_api/config_editor.py index 438e122..6f66636 100644 --- a/castle-api/src/castle_api/config_editor.py +++ b/castle-api/src/castle_api/config_editor.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +from pathlib import Path import yaml from fastapi import APIRouter, HTTPException, status @@ -58,13 +59,15 @@ class JobConfigRequest(BaseModel): config: dict -def _require_repo() -> None: - """Raise 503 if repo is not available.""" - if get_castle_root() is None: +def _require_repo() -> Path: + """Return the castle repo root, or raise 503 if it's not available.""" + root = get_castle_root() + if root is None: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Castle repo not available on this node.", ) + return root def _aggregate_yaml(config: CastleConfig) -> str: @@ -88,8 +91,7 @@ def _aggregate_yaml(config: CastleConfig) -> str: @router.get("", response_model=ConfigResponse) def get_config_yaml() -> ConfigResponse: """Get a unified virtual castle.yaml aggregated from all resource files.""" - _require_repo() - root = get_castle_root() + root = _require_repo() config = load_config(root) return ConfigResponse(yaml_content=_aggregate_yaml(config)) @@ -97,8 +99,7 @@ def get_config_yaml() -> ConfigResponse: @router.put("", response_model=ConfigSaveResponse) def save_yaml(request: ConfigSaveRequest) -> ConfigSaveResponse: """Validate and save castle.yaml. Does NOT apply changes.""" - _require_repo() - root = get_castle_root() + root = _require_repo() errors: list[str] = [] # Parse YAML @@ -119,8 +120,6 @@ def save_yaml(request: ConfigSaveRequest) -> ConfigSaveResponse: # repo: drives repo-relative source resolution (fall back to existing config) repo_path = None if data.get("repo"): - from pathlib import Path - repo_path = Path(data["repo"]).expanduser() else: try: @@ -129,8 +128,6 @@ def save_yaml(request: ConfigSaveRequest) -> ConfigSaveResponse: repo_path = None def _resolve_source(spec: ProgramSpec) -> None: - from pathlib import Path - if not spec.source: return if spec.source.startswith("repo:") and repo_path: