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

35 statements  

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

1"""Configuration settings and convenience functions for changing those settings. 

2 

3Contents: 

4 set_compatibility_rule: sets the global attribute compatibility rule. 

5 set_keyer: sets the global default function used to name dict keys. 

6 set_method_namer: sets the global default function used to name factory 

7 creation methods. 

8 set_overwrite_rule: sets the global attribute overwrite rule. 

9 set_verbose_rule: sets the global attribute message verbosity rule. 

10 _KEY_NAMER: default function that names non-`str` objects. 

11 _METHOD_NAMER: default naming convention for dispatcher creation methods. 

12 _OVERWRITE: whether to overwrite existing attributes when parameters are 

13 injected into an item. 

14 _STRICT_COMPATIBILITY: whether `is_constructor` requires a `wonka` class or 

15 accepts any object with a `create` method. 

16 _VERBOSE: whether to return more elaborate error messages and feedback. 

17 

18""" 

19 

20from __future__ import annotations 

21 

22from collections.abc import Callable 

23from typing import Any 

24 

25from . import utilities 

26 

27# Default naming function for non-str objects. 

28_KEY_NAMER: Callable[[Any], str] = utilities._namify 

29# Default naming convention for dispatcher registry creation methods. 

30_METHOD_NAMER: Callable[[Any], str] = lambda x: f'from_{x}' 

31# Whether to overwrite existing attributes when arguments are passed to create 

32# an item that is already an instance or has class attributes of the same name 

33# as in the passed arguments. 

34_OVERWRITE: bool = True 

35# Whether to validate an object as a subclass of a `wonka`-constructor or to 

36# support duck typing by not validating an object before its use. 

37_STRICT_COMPATIBILITY: bool = True 

38# Whether to return more elaborate error messages and feedback. 

39_VERBOSE: bool = False 

40 

41""" Public Functions """ 

42 

43 

44def set_compatibility_rule(compatibility: bool) -> None: 

45 """Sets the global attribute compatibility rule. 

46 

47 Args: 

48 compatibility: whether to require the `is_constructor` method to use 

49 strict or relaxed validation. 

50 

51 Raises: 

52 TypeError: if `compatibility` is not `bool`. 

53 

54 """ 

55 if not isinstance(compatibility, bool): 

56 message = 'compatibility argument must be boolean' 

57 raise TypeError(message) 

58 globals()['_STRICT_COMPATIBILITY'] = compatibility 

59 

60 

61def set_keyer(keyer: Callable[[Any], str]) -> None: 

62 """Sets the global default function used to name `dict` keys. 

63 

64 Args: 

65 keyer: function that returns a `str` name of any item passed. 

66 

67 Raises: 

68 TypeError: if `keyer` is not callable. 

69 

70 """ 

71 if not callable(keyer): 

72 message = 'keyer argument must be a callable' 

73 raise TypeError(message) 

74 globals()['_KEY_NAMER'] = keyer 

75 

76 

77def set_method_namer(namer: Callable[[Any], str]) -> None: 

78 """Sets the global default function used to name factory creation methods. 

79 

80 Args: 

81 namer: function that returns a `str` name of any item passed. 

82 

83 Raises: 

84 TypeError: if `namer` is not callable. 

85 

86 """ 

87 if not callable(namer): 

88 message = 'namer argument must be a callable' 

89 raise TypeError(message) 

90 globals()['_METHOD_NAMER'] = namer 

91 

92 

93def set_overwrite_rule(overwrite: bool) -> None: 

94 """Sets the global attribute overwrite rule. 

95 

96 Args: 

97 overwrite: whether to set the default rule to overwrite existing 

98 attributes. 

99 

100 Raises: 

101 TypeError: if `overwrite` is not `bool`. 

102 

103 """ 

104 if not isinstance(overwrite, bool): 

105 message = 'overwrite argument must be boolean' 

106 raise TypeError(message) 

107 globals()['_OVERWRITE'] = overwrite 

108 

109 

110def set_verbose_rule(verbose: bool) -> None: 

111 """Sets the global attribute message verbosity rule. 

112 

113 Args: 

114 verbose: whether to set the default rule to verbosity in logging and 

115 messaging. 

116 

117 Raises: 

118 TypeError: if `verbose` is not `bool`. 

119 

120 """ 

121 if not isinstance(verbose, bool): 

122 message = 'verbose argument must be boolean' 

123 raise TypeError(message) 

124 globals()['_VERBOSE'] = verbose