Coverage for src / lilbee / config_meta.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-04-29 19:16 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-04-29 19:16 +0000
1"""Field-set metadata derived from :class:`lilbee.config.Config`."""
3from __future__ import annotations
5import types
6from typing import Union, get_args, get_origin
8from pydantic.fields import FieldInfo
10from lilbee.config import Config
12MODEL_ROLE_FIELDS: frozenset[str] = frozenset(
13 {"chat_model", "embedding_model", "vision_model", "reranker_model"}
14)
17def _get_extra(info: FieldInfo, key: str, default: bool = False) -> bool:
18 """Read a boolean flag from a field's ``json_schema_extra``."""
19 extra = info.json_schema_extra
20 if isinstance(extra, dict):
21 return bool(extra.get(key, default))
22 return default
25def _is_nullable(info: FieldInfo) -> bool:
26 """Return True if ``None`` is part of the field's type union."""
27 origin = get_origin(info.annotation)
28 if origin is Union or origin is types.UnionType:
29 return type(None) in get_args(info.annotation)
30 return False
33def _derive_field_sets() -> tuple[
34 types.MappingProxyType[str, bool], frozenset[str], frozenset[str]
35]:
36 """Derive writable, reindex, and public field sets from Config metadata."""
37 writable: dict[str, bool] = {}
38 reindex: set[str] = set()
39 public: set[str] = set()
40 for name, info in Config.model_fields.items():
41 if _get_extra(info, "writable"):
42 writable[name] = _is_nullable(info)
43 if not _get_extra(info, "write_only") and _get_extra(info, "public", default=True):
44 public.add(name)
45 if _get_extra(info, "reindex"):
46 reindex.add(name)
47 elif name in MODEL_ROLE_FIELDS:
48 public.add(name)
49 return types.MappingProxyType(writable), frozenset(reindex), frozenset(public)
52WRITABLE_CONFIG_FIELDS, REINDEX_FIELDS, PUBLIC_CONFIG_FIELDS = _derive_field_sets()