mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-10-29 01:04:18 -04:00
* add basic pre-commit file * add flake8 * add isort * add pep585-upgrade (typing upgrades) * use namespace for import * add mypy * update ci for backend * flake8 scope * fix version format * update makefile * disable strict option (temporary) * fix mypy issues * upgrade type hints (pre-commit) * add vscode typing check * add types to dev deps * remote container draft * update setup script * update compose version * run setup on create * dev containers update * remove unused pages * update setup tips * expose ports * Update pre-commit to include flask8-print (#1053) * Add in flake8-print to pre-commit * pin version of flake8-print * formatting * update getting strated docs * add mypy to pre-commit * purge .mypy_cache on clean * drop mypy Co-authored-by: zackbcom <zackbcom@users.noreply.github.com>
45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
import inspect
|
|
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
|
|
def get_valid_call(func: Callable, args_dict) -> dict:
|
|
"""
|
|
Returns a dictionary of valid arguemnts for the supplied function. if kwargs are accepted,
|
|
the original dictionary will be returned.
|
|
"""
|
|
|
|
def get_valid_args(func: Callable) -> list[str]:
|
|
"""
|
|
Returns a tuple of valid arguemnts for the supplied function.
|
|
"""
|
|
return inspect.getfullargspec(func).args
|
|
|
|
def accepts_kwargs(func: Callable) -> bool:
|
|
"""
|
|
Returns True if the function accepts keyword arguments.
|
|
"""
|
|
return inspect.getfullargspec(func).varkw is not None
|
|
|
|
if accepts_kwargs(func):
|
|
return args_dict
|
|
|
|
valid_args = get_valid_args(func)
|
|
|
|
return {k: v for k, v in args_dict.items() if k in valid_args}
|
|
|
|
|
|
def safe_call(func, dict_args: dict, **kwargs) -> Any:
|
|
"""
|
|
Safely calls the supplied function with the supplied dictionary of arguments.
|
|
by removing any invalid arguments.
|
|
"""
|
|
|
|
if kwargs:
|
|
dict_args.update(kwargs)
|
|
|
|
try:
|
|
return func(**get_valid_call(func, dict_args))
|
|
except TypeError:
|
|
return func(**dict_args)
|