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 17:29 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 17:29 +0000
1import inspect 1G
2from collections.abc import Callable 1G
3from typing import Any 1G
6def get_valid_call(func: Callable, args_dict) -> dict: 1G
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]: 1arsbcdwefxghiyjtkzAulmBCnvDopEFq
13 """
14 Returns a tuple of valid arguments for the supplied function.
15 """
16 return inspect.getfullargspec(func).args 1arsbcdefghijtkulmnvopq
18 def accepts_kwargs(func: Callable) -> bool: 1arsbcdwefxghiyjtkzAulmBCnvDopEFq
19 """
20 Returns True if the function accepts keyword arguments.
21 """
22 return inspect.getfullargspec(func).varkw is not None 1arsbcdwefxghiyjtkzAulmBCnvDopEFq
24 if accepts_kwargs(func): 1arsbcdwefxghiyjtkzAulmBCnvDopEFq
25 return args_dict 1abcdwefxghiyjkzAlmBCnDopEFq
27 valid_args = get_valid_args(func) 1arsbcdefghijtkulmnvopq
29 return {k: v for k, v in args_dict.items() if k in valid_args} 1arsbcdefghijtkulmnvopq
32def safe_call(func, dict_args: dict | None, **kwargs) -> Any: 1G
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 true1arsbcdwefxghiyjtkzAulmBCnvDopEFq
39 dict_args = {}
41 if kwargs: 41 ↛ 44line 41 didn't jump to line 44 because the condition on line 41 was always true1arsbcdwefxghiyjtkzAulmBCnvDopEFq
42 dict_args.update(kwargs) 1arsbcdwefxghiyjtkzAulmBCnvDopEFq
44 try: 1arsbcdwefxghiyjtkzAulmBCnvDopEFq
45 return func(**get_valid_call(func, dict_args)) 1arsbcdwefxghiyjtkzAulmBCnvDopEFq
46 except TypeError:
47 return func(**dict_args)