refactor(models): lazy-load tomli_w (sub-track 2 partial)
Sub-track 2 of startup_speedup_20260606. Removes the top-level 'import tomli_w' from src/models.py and moves it inside save_config(). tomli_w (~30ms cold load) is now loaded only when the user saves config, not on every src.models import. This drops the audit violation count from 63 to 62. Pydantic BaseModel (the other src/models.py violation) is left for a future sub-track: deferring a class base requires a metaclass or proxy pattern that's higher risk for the small (~50ms) saving. 3 new tests in tests/test_models_no_top_level_tomli_w.py: - tomli_w NOT in sys.modules after import src.models - save_config() still works (because tomli_w loads on-demand) - save_config() actually triggers the import on first call 17 existing model tests pass (test_persona_models, test_bias_models, test_context_presets_models, test_per_ticket_model, test_file_item_model).
This commit is contained in:
+5
-1
@@ -43,7 +43,6 @@ import json
|
||||
import os
|
||||
import sys
|
||||
import tomllib
|
||||
import tomli_w
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
@@ -166,6 +165,11 @@ def load_config() -> dict[str, Any]:
|
||||
return tomllib.load(f)
|
||||
|
||||
def save_config(config: dict[str, Any]) -> None:
|
||||
# tomli_w is loaded on-demand (sub-track 2 of startup_speedup_20260606).
|
||||
# If it's already in sys.modules (e.g. warmed up or loaded by a prior
|
||||
# call), the import is a fast lookup; otherwise it's a cold load paid
|
||||
# only when the user actually saves config.
|
||||
import tomli_w
|
||||
config = _clean_nones(config)
|
||||
sys.stderr.write(f"[DEBUG] Saving config. Theme: {config.get('theme')}\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
Reference in New Issue
Block a user