From 820afe9319ac4b448e216897a4c0197865c8085c Mon Sep 17 00:00:00 2001 From: Will Miao Date: Mon, 2 Feb 2026 21:57:44 +0800 Subject: [PATCH] feat(recipe_scanner): ensure cache initialization and improve type safety - Initialize RecipeCache in scan_recipes to prevent None reference errors - Import PersistedRecipeData directly instead of using string annotation - Remove redundant import inside _reconcile_recipe_cache method --- py/services/recipe_scanner.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/py/services/recipe_scanner.py b/py/services/recipe_scanner.py index a9d21416..65e5bcb6 100644 --- a/py/services/recipe_scanner.py +++ b/py/services/recipe_scanner.py @@ -9,7 +9,7 @@ from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple from ..config import config from .recipe_cache import RecipeCache from .recipe_fts_index import RecipeFTSIndex -from .persistent_recipe_cache import PersistentRecipeCache, get_persistent_recipe_cache +from .persistent_recipe_cache import PersistentRecipeCache, get_persistent_recipe_cache, PersistedRecipeData from .service_registry import ServiceRegistry from .lora_scanner import LoraScanner from .metadata_service import get_default_metadata_provider @@ -431,6 +431,16 @@ class RecipeScanner: 4. Persist results for next startup """ try: + # Ensure cache exists to avoid None reference errors + if self._cache is None: + self._cache = RecipeCache( + raw_data=[], + sorted_by_name=[], + sorted_by_date=[], + folders=[], + folder_tree={}, + ) + # Create a new event loop for this thread loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) @@ -492,7 +502,7 @@ class RecipeScanner: def _reconcile_recipe_cache( self, - persisted: "PersistedRecipeData", + persisted: PersistedRecipeData, recipes_dir: str, ) -> Tuple[List[Dict], bool, Dict[str, str]]: """Reconcile persisted cache with current filesystem state. @@ -504,8 +514,6 @@ class RecipeScanner: Returns: Tuple of (recipes list, changed flag, json_paths dict). """ - from .persistent_recipe_cache import PersistedRecipeData - recipes: List[Dict] = [] json_paths: Dict[str, str] = {} changed = False