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:48 +0000

1import inspect 1p

2from collections.abc import Callable 1p

3from typing import Any 1p

4 

5 

6def get_valid_call(func: Callable, args_dict) -> dict: 1p

7 """ 

8 Returns a dictionary of valid arguments for the supplied function. if kwargs are accepted, 

9 the original dictionary will be returned. 

10 """ 

11 

12 def get_valid_args(func: Callable) -> list[str]: 1jkabcldefgmhnoi

13 """ 

14 Returns a tuple of valid arguments for the supplied function. 

15 """ 

16 return inspect.getfullargspec(func).args 1abcdefghi

17 

18 def accepts_kwargs(func: Callable) -> bool: 1jkabcldefgmhnoi

19 """ 

20 Returns True if the function accepts keyword arguments. 

21 """ 

22 return inspect.getfullargspec(func).varkw is not None 1jkabcldefgmhnoi

23 

24 if accepts_kwargs(func): 1jkabcldefgmhnoi

25 return args_dict 1jkabcldefgmhnoi

26 

27 valid_args = get_valid_args(func) 1abcdefghi

28 

29 return {k: v for k, v in args_dict.items() if k in valid_args} 1abcdefghi

30 

31 

32def safe_call(func, dict_args: dict | None, **kwargs) -> Any: 1p

33 """ 

34 Safely calls the supplied function with the supplied dictionary of arguments. 

35 by removing any invalid arguments. 

36 """ 

37 

38 if dict_args is None: 38 ↛ 39line 38 didn't jump to line 39 because the condition on line 38 was never true1jkabcldefgmhnoi

39 dict_args = {} 

40 

41 if kwargs: 41 ↛ 44line 41 didn't jump to line 44 because the condition on line 41 was always true1jkabcldefgmhnoi

42 dict_args.update(kwargs) 1jkabcldefgmhnoi

43 

44 try: 1jkabcldefgmhnoi

45 return func(**get_valid_call(func, dict_args)) 1jkabcldefgmhnoi

46 except TypeError: 

47 return func(**dict_args)