def test_hint_with_none_default_raises(self): def _test_func(a, b, c: str = None, **kwargs): pass params = {"a": 1, "b": 2, "c": 3} with pytest.raises(TypeError): signature.validate([_test_func], params)
def test_mixed_hints_raises(self): def _test_func(a: str, b, c, **kwargs): pass params = {"a": 1, "b": 2, "c": 3} with pytest.raises(TypeError): signature.validate([_test_func], params)
def test_hint_with_none_default_but_set(self): def _test_func(a, b, c: str = None, **kwargs): pass params = {"a": 1, "b": 2, "c": "foobar"} validated_params = signature.validate([_test_func], params) assert validated_params == {"a": 1, "b": 2, "c": "foobar"}
def test_hints_defaults_override(self): def _test_func(a, b, c: str = "asdf", **kwargs): pass params = {"a": 1, "b": 2, "c": "foobar"} validated_params = signature.validate([_test_func], params) assert validated_params == {"a": 1, "b": 2, "c": "foobar"}
def test_mixed_hints(self): def _test_func(a: str, b, c, **kwargs): pass params = {"a": "foo", "b": 2, "c": 3} validated_params = signature.validate([_test_func], params) assert validated_params == {"a": "foo", "b": 2, "c": 3}
def test_no_hints(self): def _test_func(a, b, c, **kwargs): pass params = {"a": 1, "b": 2, "c": 3} validated_params = signature.validate([_test_func], params) assert validated_params == {"a": 1, "b": 2, "c": 3}
def build_kwargs(kwargs: dict, functions: list, required_params: list = None) -> dict: """Build dictionary of kwargs for the union of function signatures. Args: functions (list): functions which a particular action should call required_params (list): manually defined parameters kargs (dict): kwargs provided in the event's message Returns: dict: validated kwargs required by action """ kwargs_union = {} if not required_params and functions: kwargs_union = signature.validate(functions, kwargs) elif required_params and isinstance(required_params, list): for param in required_params: param_name = param[0] if isinstance(param, tuple) else param try: # Assert required field was provided. assert param_name in kwargs except AssertionError as e: raise lpipe.exceptions.InvalidPayloadError( f"Missing param '{param_name}'") from e # Set param in kwargs. If the param is a tuple, use the [1] as the new key. if isinstance(param, tuple) and len(param) == 2: kwargs_union[param[1]] = kwargs[param[0]] else: kwargs_union[param] = kwargs[param] elif not required_params: return {} else: raise lpipe.exceptions.InvalidPayloadError( "You either didn't provide functions or required_params was not an instance of list or NoneType." ) return kwargs_union