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

25 statements  

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

1"""Shared business logic functions for `wonka`. 

2 

3Contents: 

4 finalize: finalizes construction before returning a value, including calling 

5 the `produce` method of the passed item. 

6 inject_attributes: adds keys and values of a mapping to a class or instance 

7 as attributes. 

8 is_constructor: returns `bool` as to whether an item is a `wonka`-compatible 

9 constructor. 

10 

11""" 

12 

13from __future__ import annotations 

14 

15import inspect 

16from typing import Any, TypeGuard 

17 

18from . import base, options 

19 

20""" Public Functions """ 

21 

22 

23def finalize( 

24 item: Any, 

25 parameters: base.Parameters | None = None, 

26 factory: Any | None = None) -> Any: 

27 """Modifies `item` and possibly incorporates `parameters`. 

28 

29 Args: 

30 item: item created by a factory that may need to be altered before being 

31 returned by the factory `create` method. 

32 parameters: keyword arguments to pass or add to a created instance. 

33 Defaults to `None`. 

34 factory: the constructor used to create `item`. This need not be passed 

35 if `item` is also the factory for its creation. Defaults to `None`. 

36 If `factory` is `None`, this function will look for a `produce` 

37 method on `item`. 

38 

39 Returns: 

40 Modified item. 

41 

42 """ 

43 factory = item if factory is None else factory 

44 if hasattr(factory, 'produce') and inspect.ismethod(factory.produce): 

45 return factory.produce(item, parameters) 

46 if parameters is None: 

47 return item 

48 if inspect.isclass(item): 

49 return item(**parameters) 

50 return inject_attributes(item, parameters) 

51 

52 

53def inject_attributes( 

54 item: Any, 

55 parameters: base.Parameters | None = None, 

56 overwrite: bool | None = None) -> Any: 

57 """Adds `parameters` to `item` as attributes. 

58 

59 Args: 

60 item: item to have `parameters` injected. 

61 parameters: keyword arguments to add to `item`. Defaults to `None`. 

62 overwrite: whether to overwrite existing attributes, if they exist. 

63 Defaults to `None`. If the value is `None`, the global `_OVERWRITE` 

64 setting in `options` will be used. 

65 

66 Returns: 

67 Modified item. 

68 

69 """ 

70 if parameters: 

71 overwrite = options._OVERWRITE if overwrite is None else overwrite 

72 for key, value in parameters.items(): 

73 if overwrite or not hasattr(item, key): 

74 setattr(item, key, value) 

75 return item 

76 

77 

78def is_constructor(item: Any) -> TypeGuard[base.Constructor]: 

79 """Returns if `item` is a wonka-compatible constructor. 

80 

81 If the global `_STRICT_COMPATIBILITY` setting is `True`, this function uses 

82 a narrow definition of constructor that only includes: 

83 1) subclasses or instances of `Factory`; or 

84 2) subclass instances of `Manager`. 

85 If `_STRICT_COMPATIBILITY` is `False`, the function merely tests whether 

86 `item` has a `create` method. 

87 

88 Args: 

89 item: item to test. 

90 

91 Returns: 

92 Whether `item` is a wonka-compatible constructor. 

93 

94 """ 

95 if options._STRICT_COMPATIBILITY: 

96 return ( 

97 (inspect.isclass(item) and issubclass(item, base.Factory)) 

98 or isinstance(item, base.Manager | base.Factory)) 

99 return hasattr(item, 'create') and inspect.ismethod(item.create)