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
« 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.
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.
18"""
20from __future__ import annotations
22from collections.abc import Callable
23from typing import Any
25from . import utilities
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
41""" Public Functions """
44def set_compatibility_rule(compatibility: bool) -> None:
45 """Sets the global attribute compatibility rule.
47 Args:
48 compatibility: whether to require the `is_constructor` method to use
49 strict or relaxed validation.
51 Raises:
52 TypeError: if `compatibility` is not `bool`.
54 """
55 if not isinstance(compatibility, bool):
56 message = 'compatibility argument must be boolean'
57 raise TypeError(message)
58 globals()['_STRICT_COMPATIBILITY'] = compatibility
61def set_keyer(keyer: Callable[[Any], str]) -> None:
62 """Sets the global default function used to name `dict` keys.
64 Args:
65 keyer: function that returns a `str` name of any item passed.
67 Raises:
68 TypeError: if `keyer` is not callable.
70 """
71 if not callable(keyer):
72 message = 'keyer argument must be a callable'
73 raise TypeError(message)
74 globals()['_KEY_NAMER'] = keyer
77def set_method_namer(namer: Callable[[Any], str]) -> None:
78 """Sets the global default function used to name factory creation methods.
80 Args:
81 namer: function that returns a `str` name of any item passed.
83 Raises:
84 TypeError: if `namer` is not callable.
86 """
87 if not callable(namer):
88 message = 'namer argument must be a callable'
89 raise TypeError(message)
90 globals()['_METHOD_NAMER'] = namer
93def set_overwrite_rule(overwrite: bool) -> None:
94 """Sets the global attribute overwrite rule.
96 Args:
97 overwrite: whether to set the default rule to overwrite existing
98 attributes.
100 Raises:
101 TypeError: if `overwrite` is not `bool`.
103 """
104 if not isinstance(overwrite, bool):
105 message = 'overwrite argument must be boolean'
106 raise TypeError(message)
107 globals()['_OVERWRITE'] = overwrite
110def set_verbose_rule(verbose: bool) -> None:
111 """Sets the global attribute message verbosity rule.
113 Args:
114 verbose: whether to set the default rule to verbosity in logging and
115 messaging.
117 Raises:
118 TypeError: if `verbose` is not `bool`.
120 """
121 if not isinstance(verbose, bool):
122 message = 'verbose argument must be boolean'
123 raise TypeError(message)
124 globals()['_VERBOSE'] = verbose