from typing import Callable, Optional, TypeVar, Tuple, ParamSpec, Dict, Any from functools import reduce P = ParamSpec("P") T = TypeVar("T") def is_some( callable: Callable[P, Optional[T]], *args: P.args, **kwargs: P.kwargs ) -> Tuple[bool, Optional[T]]: result = callable(*args, **kwargs) return result is not None, result def merge_dicts(*dicts: Dict[str, Any]) -> Dict[str, Any]: def merge(a: Dict[str, Any], b: Dict[str, Any]) -> Dict[str, Any]: out = dict(a) for k, v in b.items(): if k in out and isinstance(out[k], dict) and isinstance(v, dict): out[k] = merge(out[k], v) else: out[k] = v return out return reduce(merge, dicts, {})