Coverage for opt/mealie/lib/python3.12/site-packages/mealie/db/models/_model_utils/helpers.py: 89%
21 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
1import inspect
2from collections.abc import Callable
3from typing import Any
6def get_valid_call(func: Callable, args_dict) -> dict:
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]:
13 """
14 Returns a tuple of valid arguments for the supplied function.
15 """
16 return inspect.getfullargspec(func).args
18 def accepts_kwargs(func: Callable) -> bool:
19 """
20 Returns True if the function accepts keyword arguments.
21 """
22 return inspect.getfullargspec(func).varkw is not None
24 if accepts_kwargs(func):
25 return args_dict
27 valid_args = get_valid_args(func)
29 return {k: v for k, v in args_dict.items() if k in valid_args}
32def safe_call(func, dict_args: dict | None, **kwargs) -> Any:
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 true
39 dict_args = {}
41 if kwargs: 41 ↛ 44line 41 didn't jump to line 44 because the condition on line 41 was always true
42 dict_args.update(kwargs)
44 try:
45 return func(**get_valid_call(func, dict_args))
46 except TypeError:
47 return func(**dict_args)