Coverage for src/fastoai/routers/_utils.py: 86%

1 from typing import Any, get_type_hints

2

3 from pydantic import BaseModel, create_model

4 from typing_extensions import is_typeddict

5

6

7 def create_model_from(type_: Any) -> type[BaseModel]:

8 if not is_typeddict(type_):

9 raise ValueError(f"Expected TypedDict, got {type_}")

10 return create_model(

11 type_.__name__,

12 __config__=None,

13 __doc__=None,

14 __base__=None,

15 __module__=__name__,

16 __validators__=None,

17 __cls_kwargs__=None,

18 **{

19 fn: (t, ... if fn in type_.__required_keys__ else None)

20 for fn, t in get_type_hints(type_, include_extras=True).items()

21 },

22 )