Coverage for src/configuraptor/abs.py: 100%

1"""

2 Contains the Abstract config class shared by TypedConfig and BinaryConfig.

3"""

4

5 import types

6 import typing

7 from pathlib import Path

8

9# T is a reusable typevar

10 T = typing.TypeVar("T")

11# t_typelike is anything that can be type hinted

12 T_typelike: typing.TypeAlias = type | types.UnionType # | typing.Union

13# t_data is anything that can be fed to _load_data

14 T_data_types = str | Path | dict[str, typing.Any] | None

15 T_data = T_data_types | list[T_data_types]

16

17# c = a config class instance, can be any (user-defined) class

18 C = typing.TypeVar("C")

19# type c is a config class

20 Type_C = typing.Type[C]

21

22

23 class AbstractTypedConfig:

24 """

25 Logic shared by TypedConfig and BinaryConfig.

26 """

27

28 @classmethod

29 def load(

30 cls: typing.Type[C],

31 data: T_data = None,

32 key: str = None,

33 init: dict[str, typing.Any] = None,

34 strict: bool = True,

35 lower_keys: bool = False,

36 convert_types: bool = False,

37 ) -> C:

38 """

39 Load a class' config values from the config file.

40

41 SomeClass.load(data, ...) = load_into(SomeClass, data, ...).

42 """

43 from .core import load_into

44

45 return load_into(

46 cls, data, key=key, init=init, strict=strict, lower_keys=lower_keys, convert_types=convert_types

47 )