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

14 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-09-18 12:24 +0200

1""" 

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

3""" 

4 

5import types 

6import typing 

7from pathlib import Path 

8 

9# T is a reusable typevar 

10T = typing.TypeVar("T") 

11# t_typelike is anything that can be type hinted 

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

13# t_data is anything that can be fed to _load_data 

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

15T_data = T_data_types | list[T_data_types] 

16 

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

18C = typing.TypeVar("C") 

19# type c is a config class 

20Type_C = typing.Type[C] 

21 

22 

23class 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 )