Coverage for src/bobbie/options.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-10-30 23:03 +0000

1"""Settings, constants, and global types for `bobbie`. 

2 

3Contents: 

4 _FILE_EXTENSIONS: maps supported file extensions to file types. 

5 _INFER_TYPES: whether to infer the datatypes of loaded values for each 

6 file type. 

7 _LOAD_METHOD: returns the name of the `Settings` class method that loads 

8 a file type. 

9 _MODULE_SETTINGS_ATTRIBUTE: name of the variable that stores settings in a 

10 Python module. 

11 _OVERWRITE_ATTRIBUTES: whether `Settings.inject` overwrites existing 

12 attributes by default. 

13 _ALWAYS_RETURN_ERROR: whether `Settings.delete` raises an error for a 

14 missing key by default. 

15 _TYPER: function used to infer the datatypes of loaded values. 

16 _MISSING: sentinel instance for a missing value or argument. 

17 

18To Do: 

19 

20 

21""" 

22 

23from __future__ import annotations 

24 

25import dataclasses 

26from typing import TYPE_CHECKING, Any 

27 

28if TYPE_CHECKING: 

29 from collections.abc import Callable 

30 

31from . import utilities 

32 

33""" Global Variables """ 

34 

35_FILE_EXTENSIONS: dict[str, str] = { 

36 'ini': 'ini', 

37 'json': 'json', 

38 'toml': 'toml', 

39 'py': 'module', 

40 'yaml': 'yaml', 

41 'yml': 'yaml'} 

42_INFER_TYPES: dict[str, bool] = { 

43 'ini': True, 

44 'json': True, 

45 'toml': False, 

46 'module': False, 

47 'yaml': False} 

48_LOAD_METHOD: Callable[[str], str] = lambda x: f'from_{x}' 

49_MODULE_SETTINGS_ATTRIBUTE: str = 'settings' 

50_OVERWRITE_ATTRIBUTES: bool = True 

51_ALWAYS_RETURN_ERROR: bool = False 

52_TYPER: Callable[[Any], Any] = utilities._typify 

53 

54""" Missing Argument Sentinel Class and Instance """ 

55 

56 

57@dataclasses.dataclass 

58class _MISSING_VALUE: # noqa: N801 

59 """Sentinel object for a missing data or parameter. 

60 

61 This follows the same pattern as the `_MISSING_TYPE` class in the builtin 

62 dataclasses library. 

63 https://github.com/python/cpython/blob/3.10/Lib/dataclasses.py#L182-L186 

64 

65 Because None is sometimes a valid argument or data option, this class 

66 provides an alternative that does not create the confusion that a default of 

67 None can sometimes lead to. 

68 

69 """ 

70 

71 pass # noqa: PIE790 

72 

73 

74# _MISSING, instance of MISSING_VALUE, should be used for missing values as an 

75# alternative to None. This provides a fuller repr and traceback. 

76_MISSING = _MISSING_VALUE()