Coverage for src/lilbee/config_meta.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-08-14 11:46 +0000

1"""Field-set metadata derived from :class:`lilbee.config.Config`.""" 

2 

3from __future__ import annotations 

4 

5import types 

6from typing import Union, get_args, get_origin 

7 

8from pydantic.fields import FieldInfo 

9 

10from lilbee.core.config import Config 

11from lilbee.providers.roles import MODEL_ROLE_FIELDS 

12 

13 

14def _get_extra(info: FieldInfo, key: str, default: bool = False) -> bool: 

15 """Read a boolean flag from a field's ``json_schema_extra``.""" 

16 extra = info.json_schema_extra 

17 if isinstance(extra, dict): 

18 return bool(extra.get(key, default)) 

19 return default 

20 

21 

22def _is_nullable(info: FieldInfo) -> bool: 

23 """Return True if ``None`` is part of the field's type union.""" 

24 origin = get_origin(info.annotation) 

25 if origin is Union or origin is types.UnionType: 

26 return type(None) in get_args(info.annotation) 

27 return False 

28 

29 

30def _derive_field_sets() -> tuple[ 

31 types.MappingProxyType[str, bool], frozenset[str], frozenset[str] 

32]: 

33 """Derive writable, reindex, and public field sets from Config metadata.""" 

34 writable: dict[str, bool] = {} 

35 reindex: set[str] = set() 

36 public: set[str] = set() 

37 for name, info in Config.model_fields.items(): 

38 if _get_extra(info, "writable"): 

39 writable[name] = _is_nullable(info) 

40 if not _get_extra(info, "write_only") and _get_extra(info, "public", default=True): 

41 public.add(name) 

42 if _get_extra(info, "reindex"): 

43 reindex.add(name) 

44 elif name in MODEL_ROLE_FIELDS: 

45 public.add(name) 

46 return types.MappingProxyType(writable), frozenset(reindex), frozenset(public) 

47 

48 

49WRITABLE_CONFIG_FIELDS, REINDEX_FIELDS, PUBLIC_CONFIG_FIELDS = _derive_field_sets()