python-redux 0.25.2__cp314-cp314-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
redux/utils.py ADDED
@@ -0,0 +1,49 @@
1
+ """Utility functions for the project."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import inspect
6
+ from typing import TYPE_CHECKING, Any
7
+
8
+ if TYPE_CHECKING:
9
+ from collections.abc import Callable
10
+
11
+
12
+ def is_method(func: Callable) -> bool:
13
+ """Check if the function is a method."""
14
+ signature = inspect.signature(func)
15
+ return (
16
+ len(signature.parameters) > 0
17
+ and next(iter(signature.parameters.values())).name == 'self'
18
+ )
19
+
20
+
21
+ def signature_without_selector(func: Callable) -> inspect.Signature:
22
+ """Drop the parameter associated consumed by the with_store/autorun/view wrapper."""
23
+ signature = inspect.signature(func)
24
+ parameters = list(signature.parameters.values())
25
+ self_parameter: list[inspect.Parameter] = []
26
+ if is_method(func):
27
+ self_parameter = [parameters[0]]
28
+ parameters = parameters[1:]
29
+ if parameters and parameters[0].kind in [
30
+ inspect.Parameter.POSITIONAL_ONLY,
31
+ inspect.Parameter.POSITIONAL_OR_KEYWORD,
32
+ ]:
33
+ parameters = parameters[1:]
34
+ return signature.replace(parameters=self_parameter + parameters)
35
+
36
+
37
+ def call_func(
38
+ func: Callable,
39
+ injecting_args: list,
40
+ *args: tuple,
41
+ **kwargs: dict,
42
+ ) -> Any: # noqa: ANN401
43
+ """Call function `func` respecting whether it is a method or a function."""
44
+ if is_method(func):
45
+ self_ = args[0]
46
+ args_ = args[1:]
47
+ return func(self_, *injecting_args, *args_, **kwargs)
48
+
49
+ return func(*injecting_args, *args, **kwargs)