Exemplo n.º 1
0
def prepare_parameters(kwargs: Dict[str, Any],
                       method: Callable,
                       is_init: bool = False):
    """Prepares paramters passed into components before calling SDK.

    1. Determines the annotation type that should used with the parameter
    2. Reads input values if needed
    3. Deserializes thos value where appropriate
    4. Or casts to the correct type.

    Args:
        kwargs (Dict[str, Any]): The kwargs that will be passed into method. Mutates in place.
        method (Callable): The method the kwargs used to invoke the method.
        is_init (bool): Whether this method is a constructor
    """
    for key, param in inspect.signature(method).parameters.items():
        if key in kwargs:
            value = kwargs[key]
            param_type = utils.resolve_annotation(param.annotation)
            value = resolve_init_args(
                key, value) if is_init else resolve_input_args(
                    value, param_type)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                value = deserializer(value)
            else:
                value = cast(value, param_type)
            kwargs[key] = value
Exemplo n.º 2
0
def prepare_parameters(kwargs: Dict[str, Any],
                       method: Callable,
                       is_init: bool = False):
    """Prepares parameters passed into components before calling SDK.

    1. Determines the annotation type that should used with the parameter
    2. Reads input values if needed
    3. Deserializes thos value where appropriate
    4. Or casts to the correct type.

    Args:
        kwargs (Dict[str, Any]): The kwargs that will be passed into method. Mutates in place.
        method (Callable): The method the kwargs used to invoke the method.
        is_init (bool): Whether this method is a constructor
    """
    for key, param in inspect.signature(method).parameters.items():
        if key in kwargs:
            value = kwargs[key]
            param_type = utils.resolve_annotation(param.annotation)
            value = resolve_init_args(
                key, value) if is_init else resolve_input_args(
                    value, param_type)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                value = deserializer(value)
            else:
                value = cast(value, param_type)

            try:
                # Attempt at converting String to list:
                # Some parameters accept union[str, sequence[str]]
                # For these serialization with json is not possible as
                # component yaml conversion swaps double and single
                # quotes resulting in `JSON.Loads` loading such a list as
                # a String. Using ast.literal_eval to attempt to convert the
                # str back to a python List.
                value = ast.literal_eval(value)
                print(f"Conversion for value succeeded for value: {value}")
            except:
                # The input was actually a String and not a List,
                # no additional transformations are required.
                pass

            kwargs[key] = value
Exemplo n.º 3
0
    def test_get_deserializer_with_not_serializable_type(self):
        annotation = Tuple

        results = utils.get_deserializer(annotation)
        self.assertEqual(results, None)
Exemplo n.º 4
0
    def test_get_deserializer_with_serializable_type(self):
        annotation = Dict

        results = utils.get_deserializer(annotation)
        self.assertEqual(results, json.loads)