Coverage for opt/mealie/lib/python3.12/site-packages/mealie/db/models/_model_utils/helpers.py: 81%
21 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:32 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:32 +0000
1import inspect 1k
2from collections.abc import Callable 1k
3from typing import Any 1k
6def get_valid_call(func: Callable, args_dict) -> dict: 1k
7 """
8 Returns a dictionary of valid arguments for the supplied function. if kwargs are accepted,
9 the original dictionary will be returned.
10 """
12 def get_valid_args(func: Callable) -> list[str]: 1ahibcdefjg
13 """
14 Returns a tuple of valid arguments for the supplied function.
15 """
16 return inspect.getfullargspec(func).args 1abcdefg
18 def accepts_kwargs(func: Callable) -> bool: 1ahibcdefjg
19 """
20 Returns True if the function accepts keyword arguments.
21 """
22 return inspect.getfullargspec(func).varkw is not None 1ahibcdefjg
24 if accepts_kwargs(func): 1ahibcdefjg
25 return args_dict 1ahibcdefjg
27 valid_args = get_valid_args(func) 1abcdefg
29 return {k: v for k, v in args_dict.items() if k in valid_args} 1abcdefg
32def safe_call(func, dict_args: dict | None, **kwargs) -> Any: 1k
33 """
34 Safely calls the supplied function with the supplied dictionary of arguments.
35 by removing any invalid arguments.
36 """
38 if dict_args is None: 38 ↛ 39line 38 didn't jump to line 39 because the condition on line 38 was never true1ahibcdefjg
39 dict_args = {}
41 if kwargs: 41 ↛ 44line 41 didn't jump to line 44 because the condition on line 41 was always true1ahibcdefjg
42 dict_args.update(kwargs) 1ahibcdefjg
44 try: 1ahibcdefjg
45 return func(**get_valid_call(func, dict_args)) 1ahibcdefjg
46 except TypeError:
47 return func(**dict_args)